Case 1 -
String s1="world"; String s2="india"; String s3="world";
here two objects will be created in the pool. s1 and s3 point to the same object. string literals go to pool memory.
Case 2 -
String s1=new String("world"); String s2=new String("india"); String s3=new String("world");
here three objects will be created on the heap.
the new word uses heap memory to create objects.
I hope I could say.
see link for a more detailed understanding.
Nirbhay Mishra Apr 02 '13 at 10:37
source share