13: String a = "";
14: a += 2;
15: a += 'c';
16: a += false;
17: if ( a == "2cfalse") System.out.println("==");
18: if ( a.equals("2cfalse")) System.out.println("equals");
Output:
equals
Please correct me if I am wrong...
At line 13 a new String
object is created and the reference is stored at a
. (a = "")
At line 14 a new String
object is created and the reference is stored in a
. The previous String
object becomes eligible of garbage collection(GC). (a = "2c")
At line 15 a new String
object is created and the reference is stored in a
. The previous String
object becomes eligible of garbage collection(GC). (a = "2cfalse").
Now, the String pool
consists of the 2cfalse
literal. Therefore, at line-17, shouldn't a == "2cfalse"
evaluate to true
since they are both pointing to the same object in memory?
However, the program output is just ==
. Where did I go wrong? Please can someone explain it to me...