How to create string object at Heap and String constant pool using one statement in Java


We can create string object at Heap via below statement.
String  str  = new String("ABC") ;  By executing this statement "ABC" will go in Heap but not in string constant pool.
Again, we can create the same object in string constant pool via below statement.



String  str = "ABC" ;  By executing this statement "ABC " will go in string constant pool but not in Heap.
Now , If we want to create object "ABC" to be created on both String constant pool and Heap we can use the below statement .
String str = new String("ABC").intern() ; 
BY executing above statement object will be created both at String constant pool and Heap.
So , executing  String str = "ABC" ; after above statement will never create object as it always refer the object in string constant pool.

Share on Google Plus

About chetan

Howsstuff

0 comments:

Post a Comment