This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Is the memory allocated on heap is saved? I understand that it would add the literals to the string pool, moreover s1==s2 will result in true. But does it really have any advantage as far as memory used to create an object (a String object in this case) is concerned? Also, where can I get to read more abt this in details besides RHE? TIA, - Manish
Another advantage is speed of comparations between strings. If several strings to compare are interned, they can be compared with == which is faster than compareTo. Because == only compares that the memory address of both String objects is the same. They are the same because subsequent calls to intern simply return the same addres of a string allocated in the string pool that has the same content.
Jamel, If String objects having the same data are created explicitly with the new operator or their values are computed at runtime, their references will be different. Literal strings will represent the same reference if they are created by explicitly using the intern() method and the resulting string is already in the string pool. Tina