String
String is usually created in following ways:
String name="xyz";
or
String name=new String("xyz");
or
char[] n={'x','y','z'};
String name=new String(n);
There are many other constructors of String class taking arguments of different kinds.
Difference between String name="xyz" and String name=new String("xyz")
What does String name="xyz" mean?
Every object that is created in Java is created on Heap. "xyz" is a String object created on heap and 'name' is the reference variable refering to that String object.
Using this way of declaring string, the String object created is added to special area of memory in heap called the "String constant pool". JVM set aside this special area of memory to make Java more memory efficient. How? First, let us understand what is String immutability.
String are Immutable Objects
String s=name; //create a 2nd reference variable refering to the same string
s=s.concat("abc");
From the above figure we can see that a new String object("xyzabc") is created and reference variable s point towards this newly created object. An object of the string concatenated("abc") is also created but it is not referenced by any variable.
A whole new String object(xyzabc) is created after concatenation and the old object(xyz) is not changed. This is known as String Immutability i.e. a String value can never be changed.
Let's look at another example:
String x="Java";
x.concat(" Rocks!");
System.out.println("x= "+x); //the output is "x=Java"
- First line created a new String object having value "Java" and refer x to it.
- Second line created a new String object having value "Java Rocks!", but nothing refers to it. This String Object is instantly lost.
- So, the output is "Java" as reference varibale x refers to this String object only.
- If the second line is changed to x=x.concat(" Rocks!"), the output would have been: "Java Rocks!" and the String Object("Java")created in first line would have been lost as no reference variable would point to it.
Now, lets addx.toUpperCase();System.out.println("x= "+x); //the output is still: x= Java
- The String Object having value "JAVA" was created, but it was lost as x still refers to original, unchanged String "Java".
String s1="spring ";
String s2="s1+"summer";
s1.concat("fall ");
s2.concat(s1);
s1+="winter";
System.out.println(s1+" "+s2);
What is the output? How many String objects were created and lost prior to println statement?
Answer: output: spring water spring summer. Total 8 String Objects. 6 lost. "spring"(lost), "summer"(lost), "spring summer", "fall"(lost), "spring fall"(lost), "spring summer spring"(lost), "winter"(lost), "spring winter"
What does String name=new String("xyz") mean?
A String object "xyz" is created in String constant pool and another same String object "xyz" is created in normal heap memory. The reference variable "name" will point to the object that is in normal heap memory and not to the object that is in String constant pool.
Now, lets come back to our question that how 'String constant pool' makes Java more memory efficient.
Remember,
Now, lets come back to our question that how 'String constant pool' makes Java more memory efficient.
Remember,
String name="xyz";Now,
String s="xyz";
In first line, String object "xyz" is created in 'String constant pool' and reference varible 'name' is referencing to it. Now, in second line, a new String object "xyz" should be created in 'String constant pool'. But, instead the refernce variable 's' is made to point to the already available String object "xyz" and no new String object is created. The JVM looks in the String constant pool and finds out that the same String object is already available. So, it does not create a new String object with same value. Instead, it make the new reference variable refer to the already available String object.
This is how String constant pool make Java memory more efficient. Instead of creating a new String object everytime it make the reference to the old String object with same value if available and saves memory.
Let's see this and answer:
String name=new String("xyz"); and String s=new String("xyz");
How many objects will be created overall? Which object will be created where? To which object reference variables 'name' and 's' refer to?
How many objects?
Who all guessed 3 objects will be created are CORRECT or may be WRONG.
If String object "xyz" is not already availabe in String constant pool then 3 objects will be created else 2.
Where? Reference?
If String object "xyz" is not already availabe in String constant pool, then it will be created there. One String object "xyz" will be created in normal heap memory refered by 'name'. One more String object "xyz" will be created in normal heap memory refered by 's'.
Leave your queries in comment below.








No comments:
Post a Comment