String: String is class in java. It is used to hold the group of characters.
There are two ways of declaring strings:
1) String Literal
2) String Object
Example:
i) String str="JAVA";
ii) String str=new String("JAVA");
The first type of string is a String Literal.
The second type of string is an String object.
There is no difference in the content, but creating object occupies a lot of memory in your system. so JVM (Java Virtual Machine) uses some trick to reduce the cost of memory occupied by the string. JVM maintains a string Pool which contains all the unique string literals declared by your programs.
So, Whenever you declare any String literal, JVM checks the string pool if that string is present in that pool then again memory is not occupied by that string instead both the string points to one memory location.'
See this image for more information.
Fig: String Pool
For more Details Refer following links:
No comments:
Post a Comment