This question already has answers here:
What is the Java string pool and how is "s" different from new String("s")? [duplicate]
(5 answers)
Closed 7 years ago.
String s = new String("abc")
I know that this would create a new String object in Heap.
But i am confused about a statement in SCJP book by Kathy Sierra.
It states that the above statement would create an object in heap and at the same if the String "abc" is not present in the String pool , it would also add "abc" to the String pool.
Could anyone please tell me if the object "abc" is also created in the String pool in the above case.
String s = new String("abc")
Yes,above line will create two objects, one in string pool and other in Heap.
So, now
1) If you create a string literal like:-
String s1="abc"; //abc value will be taken from string pool which is previously added
2) If you create a String object and call intern method, no new object will be created instead it will just refer to "abc" present in string pool.
String s2=new String("abc").intern();
Note: When we are creating new String object based on existing one, it reuses char[] value.
Since new String(someStringValue) creates an exact copy of existing string and strings are immutable, there is clearly no reason for the two to exist at the same time.
Even if you have two String objects, they might still point to the same content.
You can refer this:- https://dzone.com/articles/string-memory-internals
Yes, it will be created as you are not having the same string before this line in your code. Now consider this example.
String temp = "abc"; // line1
String s = new String("abc"); // line2
In this case "abc" is not recreated. s will point to "abc" that was created at line 1.
sujith in comment : But in that case i do not see any difference between String temp ="abc" and String temp = new String("abc").
In line 1 a new "abc" will be created in heap. And a new String object will be created and added into the stack. And in line2 a new object of String will be created which will be referring to the "abc" that was created at line1 in heap. To better understand what thing goes to stack and what goes to heap visit here.
Yes it does. To optimize memory use, it does so.
In case you create another variable with same value "abc",
new variable will still point to earlier rather than creating a new one.
Somehow it is trying not to create two equal objects
Related
This question already has answers here:
String s = new String("xyz"). How many objects has been made after this line of code execute?
(21 answers)
What is the difference between "text" and new String("text")?
(13 answers)
Closed 5 years ago.
String str=new String ("xyz");
I read somewhere JVM create 2 object 1st in pool and 2nd in heap.
Is this true ? If true then why JVM create 2 object when one is already there can anyone explain?
Yes, you are right. It creates two objects. One in String Constant Pool and another object in Heap pointing to the String pool.
If we consider String str= "Hello" //String literal, it creates only one object by JVM in String Constant Pool.
As per your syntax(String str = new String("xyz") //String object) JVM creates 2 objects. One in string pool and another in heap).
For further reference, please check the following discussion:
String s = new String("xyz"). How many objects has been made after this line of code execute?
"xyz" is in the constant pool once the class has been loaded
str is put on the heap when that line actually runs
see
Where does Java's String constant pool live, the heap or the stack?
This question already has answers here:
How can a string be initialized using " "?
(10 answers)
Closed 6 years ago.
I am new to stackoverflow which I am finding it very useful, thanks so much for a great community. While I've been understanding about Strings in java, I just wanted to know how are Strings literals source created? I understood most of the concept of Strings in java. It is a special kind of class that is final, I also understood when creating a String literal, an object will be created on the heap and its reference will be in the string constant pool. by using this.
String name = "Manchester";
I also understood that when a string is created using new operator, the reference will have the object on the heap and a reference in the string constant pool. By this way.
String name = new String("United");
As I did understand how the object was created on the heap using the new operator, what I didnt understand is how the object is created on the heap when we use double quotes " ". How is this accomplished? I went thought the source code of String.class too but it wasn't of any help for me.
Can anyone please let me know how this works? Thanks.
String name = "Manchester";
will search in constant pool of string(in heap) for the same valued object and if found will use that, if not will do this :
String name = new String("Manchester");
Do note that String is immutable, so it tries to use already present objects rater than creating one.
You can check it in this way:
String x = "a";
String y = "a";
x==y //true
And
String x = new String("a");
String y = new String("a");
x==y //false
This question already has answers here:
String s = new String("xyz"). How many objects has been made after this line of code execute?
(21 answers)
Closed 7 years ago.
I was going through SCJP examination and found one line in one book.
String s = new String("abc");
And it was written that, two objects will be created on above line. one on HEAP and one on STRING POOL.
I am not satisfied by the declaration given there. Can someone make me understand why two objects are created ?
Thanks in advance.
Author is correct. When ever you used String literal, that literal goes to constant pool first.
Hence "abc" created in constant pool.
And when you use new key word a new Object called s created on heap.
The literal "abc" is created and interned ("string pool").
The new operator will create a new String that is not interned.
Author is correct:
one object will be created in the string pool for "abc" literal; and
another object will be created on the heap for new String(...)
Object 1 - "abc"
Object 2 - new String("abc")
If I run,
String s1="abc";
then are there any differences between:
String s2=new String(s1);
and
String s2="abc";
Here's what I'm confused about:
The Head First Java says:"If there's already a String in the String pool with the same value, the JVM doesn't create a duplicate, it simply refers your reference variable to the existing entry. " Which at my point of view is that the s1 has already created "abc",s2 just refers to it. Am I right??
When you write String s2="abc"; and "abc" is already in the pool, then you won't get a duplicate - you'll get a reference to the existing String.
But if you write new String(something), you get a new String, whether there's a matching String in the pool or not.
String Constant Pool comes into picture in this case as shown in below screenshot.
I think it will help you to understand it visually.
String s1="abc"; // s1 will go to String constant pool
String s2=new String(s1); // any object created by "new" keyword will go to Heap
String s2="abc"; // s1 and s2 both will refer to same string in constant pool
the new keyword will force to create a new string object in heap even it already exist in string pool
This question already has answers here:
How many objects are being created? [duplicate]
(2 answers)
Closed 9 years ago.
How many string objects will be created by the following code?
String s="";
s+=new String("a");
s+="b";
I had this question at exam. I want to know the right answer . I said 2 objects.
The object from pool that contains "" , "b" and the object created by new String("a");
I'll anwser to another, clearer question: how many String instances are involved in the following code snippet:
String s="";
s+=new String("a");
s+="b";
And the answer is 6:
the empty String literal: "";
the String literal "a";
the copy of the String literal "a": new String("a");
the String created by concatenating s and the copy of "a";
the String literal "b"
the String created by concatenating s and "b".
If you assume that the three String literals have already been created by previously-loaded code, the code snippet thus creates 3 new String instances.
String s="";
creates no objects.
s+=new String("a");
creates five objects. the new String, the StringBuilder and its char[] and the String resulting and its char[]
s+="b";
creates four objects, the StringBuilder and its char[] and the String resulting and its char[]
So I get a total of nine objects of which are three String objects
Note: You can be sure that "" has already been loaded as it appear in many system classes including ClassLoader and Class.
The Strings "a" and "b" may or may not be considered as new Strings for the purpose of this question. IMHO I wouldn't count them as they will only be created at most once and if this code is only run once, it hardly matters how many strings are created. What is more likely to be useful is to know how many objects are created each time the code is run.
The number of objects actually created in a JITC situation is indeterminate. The JITC may well recognize that new String("a") is an identity, and that no intermediate values of s are referenced, so that only the StringBuilder is created. There are several potential side-effects that must be mimicked in the general case (eg, where the argument to new String() may be invalid), but with literals they can't occur.
In fact, javac could very well recognize that the result is "ab", with no potential side-effects, and just produce a String literal of that value. (It does string combining in slightly less complicated cases.)
Creating New Strings
Earlier we promised to talk more about the subtle differences between the various methods of creating a String. Let's look at a couple of examples of how a String might be created, and let's further assume that no other String objects exist in the pool:
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.
From SCJP Sun Certified Programmer for Java 6 Study Guide (Exam 310-065).pdf