In an interview question, Interviewer asked me
What is the common and difference between the following statements:
String s = "Test";
String s = new String("Test");
Is there any difference in memory allocation?
In an interview question, Interviewer asked me
What is the common and difference between the following statements:
String s = "Test";
String s = new String("Test");
Is there any difference in memory allocation?
String s = "Test";
Will first look for the String "Test" in the string constant pool. If found s will be made to refer to the found object. If not found, a new String object is created, added to the pool and s is made to refer to the newly created object.
String s = new String("Test");
Will first create a new string object and make s refer to it. Additionally an entry for string "Test" is made in the string constant pool, if its not already there.
So assuming string "Test" is not in the pool, the first declaration will create one object while the second will create two objects.
The differnce in term of memory is that the expressions of the form :
String s = "test"
uses the "interned" string so as to share unique instances.
The invocation of form: String s = "test"
is efficient compared to String s = new String("test")
The first call makes use of the existing constant expression (if there is one), the second call creates a new instance without making use of any existing instance.
Below code chunk demonstrates this:
String test = new String("test");
String internTest = "test";
String nonInternTest = new String("test");
System.out.println(test == internTest); // prints false
System.out.println(test != nonInternTest); // prints true
System.out.println(test.equals(nonInternTest)); // prints true
Also note that the JLS specifies the behavior to be thus:
Each string literal is a reference to an instance of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions are "interned" so as to share unique instances, using the method String.intern.
String s = "Test"; // creates one String object and one reference variable In this simple case, "Test" will go in the pool and s will refer to it.
String s = new String("Test"); // creates two objects and one reference variable In this case, because we used the new keyword, Java will create a new String object in normal (non-pool) memory, and s will refer to it. In addition, the literal "Test" will be placed in the pool.
but what they have in common is that they all create a new String object, with a value of "Test", and assign it to a reference variable s.
First one will search the String literal "Test" in the String pool, if it is present then s will refer that and will not create new one. and creates new object only when "Test" literal is not there.
but in the second case it will create an another object without bothering much about whether it is present or not.
+The differnce between creating a new String objectand creating reference is nothing but we are telling to jvm to create a new object. and create the refence means we are creating the object our self only.
String s=new String("Test")
In this case 2 objects will be created one in the heap area and another is in scp (String constant Pool) and s
is always pointing to heap Object .
String s ="Test";
In this case only 1 Object will be created in scp and s
is always pointing to that object
Note
1.Object creation in scp is optional first it will check is there any Object already present in scp with required content if Object already present then existing Object will be reused if Object not already available then only a new Object will be created. But this only applicable for scp but not for the heap.
2.Garbage collector is not allowed to access SCP area hence even though Object doesnt contain reference variable it is eligible for GC if it is present in scp Area
All scp Objects will be destroyed automatically at the time of jvm shutdown.
Example 2;
String s1=new String("Test");
String s4=new String("Test");
String s2="Test";
String s3="Test";
Note:
Whenever we are using new operator compulsory a new object will be created in the heap area hence there may be a chance of existing 2 Objects with same content in heap area but not in scp, that is duplicate objects are possible in the heap area but not in scp.