Wednesday, 18 November 2015

Difference between the ways of creating String Object? (or) Diff b/w String literal and String object

There are mainly 2 ways of creating String objects:
String s = "GetSure" : One String object will create in String constant pool and S is referring to that object.
String s = new String("GetSure") : One String object is created on the heap and s is pointing to that object.Now JVM will check is there any object present in the constant pool with this content, if there is no such object JVM will keep another object in the String constant pool with this content. This object is reserved for future purpose.

No comments:

Post a Comment