Java Object Assignment

You are doing completely different things in the two cases. In the first case, with t1.i = 1;, you are modifying the object pointed to by t1. In the second case, with t1 = "1";, you are changing the reference to point to another object (similar to when you did t2 = t1;.

If you do the same thing to Test that you did in the second case, you would get the same result (assuming Test has a constructor that takes an integer):

Test t1 = new Test(5);
Test t2 = t1;          
t2 = new Test(1); // Here we are assigning to the variable, just like your 2nd example
System.out.println(t1);
System.out.println(t2);

People mention that String is immutable. But that is irrelevant and there is no concept of "mutability" in the language and there is no difference between how "mutable" and "immutable" classes work. We informally say a class is "immutable" if it happens to not have any fields you can set or any methods that can change its internal content. Such is the case with String. But a mutable class would work the exact same way if you simply do not do anything to mutate it.


You are right, but Strings are a special case; they are immutable and act like primitives in this case.

@newacct

I quote http://docs.oracle.com/javase/tutorial/java/data/strings.html :

Note: The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.

This is what makes strings a special case. If you don't know this, you might expect the methods discussed in the quote not to return new strings, wich would lead to unexpected results.

@user1238193

Considering your following question: "Does Test t2 = t1; cause both t2 and t1 point to the same object or each now have their own objects? Does this situation applies on all objects on Java?"

t1 and t2 will point to the same object. This is true for any java object (immutable objects included)


You are right with your first assumption. With the following line of code:

Test t1 = new Test();

you create a new Test object, and in the same time you create a Test reference named t1 to refer to it.

At the second line of the code you posted:

Test t2 = t1;

You are actually creating another Test reference, and you assign it to refer to the same object that t1 refers to.

So t1.i = 1; will affect t2.i for it is the same object after all.

As for the Strings, Strings are immutable and can not be modified after instantiated.

Regarding your edit:

String s1 = "0";
String s2 = s1;          
s1 = "1";
System.out.println(s1);
System.out.println(s2);

They will print different results, because when you actually say

s1 = "1";

you are actually binding s1 to another String object, but s2 will still be refering to the object with value "0".