ARTICLE AD BOX
new String("test") always creates a new object on the heap.
So in:
String a = new String("test"); String b = new String("test");Java creates two different String objects, even though the text is the same.
Therefore:
because == compares references, and a and b point to different objects.
equals() returns true because String overrides it to compare content, not memory location.
2 Comments
(JLS 15.9.4: " The value of a class instance creation expression is a reference to the newly created object of the specified class. Every time the expression is evaluated, a fresh object is created. ")
2026-01-09T06:46:25.543Z+00:00
