This has to be a duplicate of a large number of questions, but I will comment by saying that when you do the following:
String a = "abc";
String b = "abc";
The JVM creates a single String object in the constant pool which contains the String abc
. Hence, the a
and b
Strings simply point to the same string in the pool.
However, when you do the following:
String a = "abc";
String b = new String("abc");
a new object is created even though abc
already exists in the pool. Hence the comparison a == b
returns false, although the contents of both these strings remains equivalent.