Hi,
Got the answer to that one.
String x = new String ("xyz");
is equivalant to
char data[] = {'x', 'y', 'z'}; //--> Array Object #1
String x = new String (data); //--> String Object #2
That's two objects.
That's not quite how it works. The original line of code contains a String literal. When the class is loaded, a String object is created on the heap based on the character sequence "xyz", and a reference to this String is retained in a pool. When the method containing this line is run, a new String is created on the heap, containing the same character sequence as the previously created String object. After this code executes, there are two Strings on the heap, each containing the character sequence "xyz".
If you compare the object references with x == "xyz", you'll find it returns false, meaning the references point to different objects.
In practical code, lines such as the one above (new String("whatever"))are impractical and wasteful, as they create unnecessary String objects. It's useful for
testing purposes, however, to know what's going on, and that such a line results in two separate String objects.
Hope this helps!