Tuesday, May 24, 2011

The assignment operators in java

The java assignment operator statement has the following syntax:

<variable> = <expression>

If the value already exists in the variable it is overwritten by the assignment operator (=).

j = 10; // j gets the value 10.
j = 5; // j gets the value 5. Previous value is overwritten.
k = j; // k gets the value 5.

// Multiple Assignments
k = j = 10; // (k = (j = 10))


But in case of reference, just a shallow copy is made:


SomeClass obj1 = new SomeClass();
SomeClass obj2 = new SomeClass();
obj2 = obj1;


obj2 and obj1 are 2 references pointing to the same object, which was initialized in 1st statement by obj1.

No comments:

Post a Comment

Chitika