According to Kathy Sierra's book:
String s = "abc"; // creates one String object and one
// reference variable
In this simple case, "abc" will go in the pool and s will refer to it.
String s = new String("abc"); // 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 (nonpool) memory, and s will refer to it. In addition,the literal "abc" will be placed in the pool.
As I know, when we use literals, it stores value in pool but when we use new keyword it creates object in heap. Here I want to know, is the heap object refer pool data (String) or not?
The variable you created using new String("abc") is not the same as the pool "abc" string.
String string1 = "abc";
String string2 = new String("abc");
System.out.println(string1.equals(string2)); //will print true
System.out.println(string1 == string2); //will print false
Related
As per Java,
There are two places in which strings are stored. String literal pool and heap memory according to its creation. I need to know, when assignment of string is done to another string, where does the newly created string will be stored?
I've done the assignment operations on both the type of Strings which are there in heap as well as in string pool. I got results like this.
String str = new String("foo"); ===> str is created in heap memory
String strL = "doo"; ===> strL is created in String pool.
But when,
String strNew = strL; // strL which is there in String Pool is assigned to strNew.
Now, If I do this
strL = null;
System.out.println(strNew)// output : "doo".
System.out.println(strL)// output : null.
Similarly,
String strNewH = str; // str which is there in heap is assigned to strNewH
Now,
str = null;
System.out.println(strNewH);// output : "foo".
System.out.println(str); // null
Above is the output I have got on IDE.
According to this output, a new referenced object for strNew is created in string pool and a new referenced object for strNewH is created in heap. Is it correct?
You have a few misconceptions.
The string pool is also part of the heap. You probably meant to ask whether the strings are in the string pool or some other part of heap.
You also seem to think that assignments create new objects. They don't. Variables and objects are separate things. After these two lines:
String str = new String("foo");
String strL = "doo";
The variable str refers to a string object "foo", that is not in the string pool. The variable strL refers to a string object "doo", that is in the string pool.
(Note the word "refers")
When you assign String variables, you are simply changing what they refer to. Here:
String strNew = strL;
You are making strNew refer to the same object as the object that strL refers to.
Similarly, when you set something to null, you make it refer to nothing. The object it was referring to is not necessarily destroyed.
So as for your question:
According to this output, a new referenced object for strNew is created in string pool and a new referenced object for strNewH is created in heap. Is it correct?
No, it is not correct. There are no new objects created. strNew refers to "doo", which is in the string pool, and is the same object as the one that strL was referring to. strNewH refers to "foo", which is not in the string pool, and is the same object as the one that str was referring to.
How many distinct String object instances are created in the following code segment?
String s1 = new String("hello");
String s2 = "GoodBye";
String s3 = s1;
Not sure about all my reasoning here.
By using the keyword new that creates an instance from the String class, I am guessing that has to be an object. However, I am confused, is String after the new now considered a method because it has ( ) and then it is calling a String literal "hello" in it?
String s2 = "Goodbye";
I think this is a String literal, and since Strings are actually objects even the String literal is considered object. Not 100% sure if that is true.
String s3 = s1; Just refers back to s1. Therefore, it is not distinct.
So my answer is 2 distinct objects.
Please explain if I am a right or wrong.
The correct answer is 3.
String s1 = new String("hello");
String s2 = "GoodBye";
String s3 = s1;
The compiler will put both literals "hello" and "GoodBye" into a "constant pool" during compilation time, which then will be loaded by the classloader. So the JVM automatically interns all String literals used by this class, when it loads that class. More about this: When are Java Strings interned?. The String constant pool is then managed during runtime.
During runtime the JVM will create the third String object when it reaches the line String s1 = new String("hello").
So you would and up with three distinct String objects, where two of them contain the same word "hello". So s1.equals("hello") would be true, but s1 == "hello" would be false, because s1 references to a different String on the heap, than the literal "hello".
The line String s3 = s1 just creates a variable s3 with a copied reference to the String object of s1. It doesn't create a new object.
Also mind that you can "manually" add Strings into the String constant pool by using the method String#intern. So s1.intern() == "hello" is true, because the String reference returned from s1.intern() is the reference to the literal "hello" which was already in the constant pool.
If you like to get another and maybe more detailed explanation with some drawings about objects and their location, you can check this article on javaranch.
So my question is in relation to declaring and assigning strings.
The way I usually declare strings is by doing the following:
String s1 = "Stackoverflow";
And then if I ever need to change the value of s1 I would do the following:
s1 = "new value";
Today I found another way of doing it and declaring a string would go like:
String s2 = new String("Stackoverflow");
And then changing the value would be:
s2 = new String("new value");
My question is what is the difference between the two or is it simply preferential. From looking at the code the fourth line
s2 = new String ("new value");
I'm assuming that doing that would create a new memory location and s2 would then point to it so I doubt it would be used to change the value but I can see it being used when declaring a string.
From the javadoc :
Initializes a newly created String object so that it represents the
same sequence of characters as the argument; in other words, the newly
created string is a copy of the argument string. Unless an explicit
copy of original is needed, use of this constructor is unnecessary
since Strings are immutable.
So no, you have no reason not to use the simple literal.
Simply do
String s1 = "Stackoverflow";
Historically, this constructor was mainly used to get a lighter copy of a string obtained by splitting a bigger one (see this question). Now, There's no normal reason to use it.
String s1 = "Stackoverflow"; // declaring & initializing s1
String s2 = new String("Stackoverflow"); // declaring & initializing s2
in above cases you are declaring & initializing String object.
The difference between
//first case
String s2 = new String("new String"); // declaring & initializing s2 with new memory
// second case
s2 = "new String" // overwriting previous value of s2
is in the first case you are creating a new object, i-e; allocating memory for a new object which will be refrenced by s2. The previous address to which s2 was pointing/referencing hasn't released the memory yet which will be gc when the program ends or when system needs to.
The good programming practice (second case) is to initialize an object once, and if you want to change its value either assign null to it and then allocate new memory to it or in case of string you can do like this
s2= "new String";
String s1 = "Stackoverflow";
This line will create a new object in the String pool if it does not exist already. That means first it will first try searching for "Stackoverflow" in the String pool, if found then s1 will start pointing to it, if not found then it will create a new object and s1 will refer to it.
String s2 = new String("Stackoverflow");
Will always create a new String object, no matter if the value already exists in the String pool. So the object in the heap would then have the value "Stackovrflow" and s2 will start pointing to it.
s2 = new String("new value");
This will again create a new Object and s2 will start pointing to it. The earlier object that s2 was pointing to is not open for garbage collection (gc).
Let me know if this helps.
The main difference is that the constructor always creates a totally new instance of String containing the same characters than the original String.
String s1 = "Stackoverflow";
String s2 = "Stackoverflow";
Then s1 == s2 will return true
String s1 = "Stackoverflow";
String s2 = new String("Stackoverflow");
Then s1 == s2 will return false
Just using the double quotes option is often better:
With a constructors you might create two instance of String.
Easier to read and less confusing
#user2612619 here i want to say is .. when ever ur creating object with "new" operator it always falls in heap memory . so wenever u have same content but different objects than also it will create new objects on heap by this u cant save memory ....
but in order to save memory java people brought concept of immutable where we can save memory .. if u r creating a different object with same content .. string will recognize dat difference and creates only one object with same content and points the two references to only one object ..
i can solve ur doubts from this figure ..
case 1:
String s = new String("stackoverflow");
String s1 = new String("stackoverflow");
as they are two different objects on heap memory with two different values of hashcode .. so s==s1 (is reference comparison) it is false .. but s.equals(s1) is content comparison ..so it is true
case 2:
String s = "stackoverflow";
String s1 = "stackoverflow";
here objects fall in scp(string constant pool memory)
same object for two different refernces .. so hashcode also same .. same reference value .. so s==s1 is true here [u can observe from figure clearly]
s.equals(s1) is true as content comparison.. this is very beautiful concept .. u will love it wen u solve some problems ... all d best
Creating String object Directly:
String s1 = "Hip Hop"
will create a string object but first JVM checks the String constant or literal pool and if the string does not exist, it creates a new String object “Hip jop” and a reference is maintained in the pool. The variable s1 also refers the same object. Now, if we put a statement after this:
String s2 = "Hip Hop"
JVM checks the String constant pool first and since the string already exists, a reference to the pooled instance is returned to s2.
System.out.println(s1==s2) // comparing reference and it will print true
java can make this optimization since strings are immutable and can be shared without fear of data corruption.
Creating String Object using new
String s3 = new String("Hip Hop")
For new keyword, a String object is created in the heap memory wither or not an equal string object already exists in the pool and s3 will refer to the newly created one.
System.out.println(s3==s2) // two reference is different, will print false
String objects created with the new operator do not refer to objects in the string pool but can be made to using String’s intern() method. The java.lang.String.intern() returns an interned String, that is, one that has an entry in the global String literal pool. If the String is not already in the global String literal pool, then it will be added to the pool.
String s4 = s3.intern();
Systen.out.println(s4 == s2); // will print `true` because s4 is interned,
//it now have the same reference to "Hip Hop" as s2 or s1
But try:
Systen.out.println(s4 == s3) // it will be false,
As the reference of s4, s2 and s1 is the reference to the pooled instance while s3 is referring to the created object in heap memory.
use of new for creting string:
prior to OpenJDK 7, Update 6, Java String.susbtring method had potential memory leak. substring method would build a new String object keeping a reference to the whole char array, to avoid copying it. You could thus inadvertently keep a reference to a very big character array with just a one character string. If we want to have minimal strings after substring, we use the constructor taking another string :
String s2 = new String(s1.substring(0,1));
The issue is resolved in JDK 7 update 6. So No need to create string with new any more for taking advantage provided by String literal pool mechanism.
Referance:
String literal pool
using new to prevent memory leak for using substring
This question already has answers here:
Questions about Java's String pool [duplicate]
(7 answers)
Closed 9 years ago.
Can string pool contain two strings with the same value??
String str = "abc";
String str1 = new String("abc");
Will the second statement with `new()` operator creates two objects of `string` "abc", one on `heap` and another on `string` pool?
Now if i call intern() on str1 ie str1.intern(); as a third statement, will str1 refer to the "abc" from String pool?
If yes then what will happen to the object that was created on heap earlier by the new(). Will that object be eligible for garbage collection.?
If no then what will be the result of str1.intern();?
No first will also create one object and also second one will create only one string object. Difference is that first one will create in String pool and second one will create in heap only.
If you will call str1.intern(); then it will be added to String pool.
String str1 = "abc";
String str2 = new String("abc");
Stirng str3 = "abc"
Here two objects will be created. first line will create one strong object with reference str1 and 3rd line will point to the same object created in 1st line with reference str3 but in 2nd line one new object will be created as we are using new keyword here. Hope it will help you.
Also check this answer. Good explanation is there.
"abc" object will be created on class loading and put on String pool. The second line will use String(String original) constructor where original is the pointer to "abc" in pool. This is the bytecode for the second line:
NEW java/lang/String
DUP
LDC "abc"
INVOKESPECIAL java/lang/String.<init>(Ljava/lang/String;)V
ASTORE 2
new String("abc") is a class instance creation expression, and must create a new object. Whether it shares the same char array internally as the literal "abc" is up to the String implementation. Both the "abc" references will use the same intern String.
String t1 = "ABC";
String t2 = new String("ABC");
String t3 = "ABC";
System.out.println(t1 == t2);
System.out.println(t2 == t3);
System.out.println(t1 == t3);
generates the following output (Java SE 7):
false
false
true
It is basically :
// "abc" is interned by the JVM in the pool
String str="abc";
// JVM uses the pooled "abc" to create a new instance
String str1=new String("abc");
Only one new String object is created referenced by str1 . The String literal "abc" was interned by the JVM. Since the literal "abc" already existed in the String pool , in the second statement , JVM should use that .
class A {
String s4 = "abc";
static public void main(String[]args ) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
A o = new A();
String s5 = new String("def");
System.out.println("s1==s2 : " + (s1==s2));
System.out.println("s1==s1.intern : " + (s1==s1.intern()));
System.out.println("s1==s3 : " + (s1==s3));
System.out.println("s1.intern==s3.intern : " + (s1.intern()==s3.intern()));
System.out.println("s1==s4 : " + (s1==o.s4));
}
}
The output:
s1==s2 : true
s1==s1.intern : true
s1==s3 : false
s1.intern==s3.intern : true
s1==s4 : true
My questions:
1.What happens for "String s1 = "abc"? I guess the String object is added to the pool in class String as an interned string? Where is it placed on? The "permanent generation" or just the heap(as the data member of the String Class instance)?
2.What happens for "String s2 = "abc"? I guess no any object is created.But does this mean that the Java Intepreter needs to search all the interned strings? will this cause any performance issue?
3.Seems String s3 = new String("abc") does not use interned string.Why?
4.Will String s5 = new String("def") create any new interned string?
The compiler creates a String object for "abc" in the constant pool, and generates a reference to it in the bytecode for the assignment statement.
See (1). No searching; no performance issue.
This creates a new String object at runtime, because that is what the 'new' operator does: create new objects.
Yes, for "def", but because of (3) a new String is also created at runtime.
The String objects at 3-4 are not interned.
1.What happens for "String s1 = "abc"?
At compile time a representation of the literal is written to the "constant pool" part of the classfile for the class that contains this code.
When the class is loaded, the representation of the string literal in the classfile's constant pool is read, and a new String object is created from it. This string is then interned, and the reference to the interned string is then "embedded" in the code.
At runtime, the reference to the previously created / interned String is assigned to s1. (No string creation or interning happens when this statement is executed.)
I guess the String object is added to the pool in class String as an interned string?
Yes. But not when the code is executed.
Where is it placed on? The "permanent generation" or just the heap(as the data member of the String Class instance)?
It is stored in the permgen region of the heap. (The String class has no static fields. The JVM's string pool is implemented in native code.)
2.What happens for "String s2 = "abc"?
Nothing happens at load time. When the compiler created the classfile, it reused the same constant pool entry for the literal that was used for the first use of the literal. So the String reference uses by this statement is the same one as is used by the previous statement.
I guess no any object is created.
Correct.
But does this mean that the Java Intepreter needs to search all the interned strings? will this cause any performance issue?
No, and No. The Java interpretter (or JIT compiled code) uses the same reference as was created / embedded for the previous statement.
3.Seems String s3 = new String("abc") does not use interned string.Why?
It is more complicated than that. The constructor call uses the interned string, and then creates a new String, and copies the characters of the interned string to the new String's representation. The newly created string is assigned to s3.
Why? Because new is specified as always creating a new object (see JLS), and the String constructor is specified as copying the characters.
4.Will String s5 = new String("def") create any new interned string?
A new interned string is created at load time (for "def"), and then a new String object is created at runtime which is a copy of the interned string. (See previous text for more details.)
See this answer on SO. Also see this wikipedia article on String Interning.
String s1 = "abc"; creates a new String and interns it.
String s2 = "abc"; will drag the same Object used for s1 from the intern pool. The JVM does this to increase performance. It is quicker than creating a new String.
Calling new String() is redundant as it will return a new implicit String Object. Not retrieve it from the intern pool.
As Keyser says, == compares the Strings for Object equality, returning true if they are the same Object. When comparing String content you should use .equals()