In Java, does `String s = "lol";` create two objects? [duplicate] - java

This question already has answers here:
Java Strings and StringPool
(2 answers)
Closed 9 years ago.
I have heard that two objects are created when you execute String s = new String("lol");. One object is created for the string constant pool and one for s on the heap.
So, are 2 objects created when we execute the following? String s = "lol"; Is the object creation the same?
Edit:
how many objects are created by :
String s1 = new String("lol1");
and how many by :
String s2 = "lol2";

No, with String s = "lol";, only one object is created. With every string literal, a String object is created and placed in the string pool. Here, s just refers to that pooled string. When you say s = new String("lol"), the string literal is created and pooled, and another string is allocated and assigned to s, which is a different, yet equal, string.
UPDATE
I had forgotten about the char[] that is used internally by a String object.
String s1 = "lol";
2 objects are created, the char[] that holds {'l', 'o', 'l'} and the String object that references it internally. It's interned in the string pool.
String s2 = new String("lol");
3 objects are created. First, the string literal: 2 objects are created, the char[] that holds {'l', 'o', 'l'} and the String object that references it. It's interned in the string pool as before. Then, the new String object that gets assigned to s2: A new String is created, but it references the same char array as the original string. Two String objects, and one char[] object. (The String(String) constructor may make a copy of the char[] in the circumstance that the original string's array's length is somehow greater than its count, but that doesn't appear to be the case here.)
Grepcode for java.lang.String(String)

"lol" is a String literal - when you reference it in your code, you force Java to create this object. The second object you're seeing is when you explicitly call String's constructor with the new operator. Assigning these values to other variables does not create additional objects.

Related

How many objects will be created in string pool?

How many objects will be created in the following Java code:
String s = "abc";
s = "";
String s2 = new String("mno");
s2 = "pqr";
String s = "abc"; → one object, that goes into the string pool, as the literal "abc" is used;
s = ""; → one empty string ("") object, and again - allocated in the string pool;
String s2 = new String("mno"); → another object created with an explicit new keyword, and note, that it actually involves yet another literal object (again - created in the string pool) - "mno"; overall, two objects here;
s2 = "pqr"; → yet another object, being stored into the string pool.
So, there are 5 objects in total; 4 in the string pool (a.k.a. "intern pool"), and one in the ordinary heap.
Remember, that anytime you use "string literal", JVM first checks whether the same string object (according to String::equals..()) exists in the string pool, and it then does one of the following:
If corresponding string does not exist, JVM creates a string object and puts it in the string pool. That string object is a candidate to be reused, by JVM, anytime equal to it (again, according to String::equals(..)) string literal is referenced (without explicit new);
If corresponding string exists, its reference is just being returned, without creating anything new.

Java String object creation

I have been reading Java String object and I had this question -
String x="a";
String y="b";
Does it create two objects in Java?
Those two lines of code will not create any objects. String literals such as "a" are put in the string pool and made available upon class loading.
If you do
String x = new String("a");
String y = new String("b");
two objects will be created in runtime.
These questions/answers should cover follow-up questions:
Questions about Java's String pool
How many Java objects are generated by this - new String("abcd")
When ever a String is initialized using new operator its new object is created.
Like if you do
String s1= new String ("string");
String s2=new String ("string");
String s3=new String ("string");
All of the three will create a separate String object in the heap.
Whereas if all the above strings are initialized without new operator then, firstly the string will be checked in string pool for its existence. If required string exist then the new reference will start pointing to the existing string.Otherwise it will create new sting in the pool. For example:
String s1= "string";
String s2="string";
String s3="string1";
In the above example only two string will be created in string pool ("string" and "string1"). Where String s1 and s2 will refer to single object "string" and s3 will refer to another string object "string1".
String with literals gets created in String Pool. whereas String through new operators gets created in Heap Memory.
Advantage of creating String through literals is if that String value is already available in String Pool then you get the same reference where through new operator everytime you create a new object new reference.
In your case you will get same reference. so only object.
String object will be created by each line, unless they already exist in string pool...if they exist in string pool only a reference will be linked to your variable and no new objects will be created.

Confusion in declaring String Objects [duplicate]

This question already has answers here:
What is the difference between "text" and new String("text")?
(13 answers)
Closed 6 years ago.
If I declare a String as
String test=new String("testing");
and
String test1="testing1"
Since String is a class in JAVA how does test1 be a String Object without using a new Operator.Also,when a new Operator is used memory is assigned for new String("testing") so in the case of test1 how is the memory assigned?
Also,when the string is interned ,if two Strings have the same value with what reference is the String store once in the String intern pool?
Let us first consider this String test=new String("testing");
It creates an String Object in Heap.No Checking is done in String Pool for existence of this String in the pool.
and now this String test1="testing1"
It creates String a String Object in String Pool not in Heap.Before Creation check is done whether this string is already there in the pool.If yes its reference is returned else a new String is created in the pool and its reference is returned.Basically this is a String Literal, which is put in Constant pool for memory optimization and re-usability.
intern(): It is used when you construct an object using new() and you call intern() on that object then first a check is done in Stirng pool if that String already exists there or not,if yes it is directly used
Java has a separate memory for Strings that are created without calling the constructor with new. Every time such a String is created Java checks if that String is already in this memory. If it is, then Java sets the same reference to the new String until one of them changes.
When you create a String with the constructor using new then it behaves as a normal object in Java.
Take a look at this example:
String s1 = "Test";
String s2 = "Test";
When you compare this String with the == operator it will return true. s1.equals(s2) will also return true.
It looks different if you create String objects with the constructor like this:
String s1 = new String("Test");
String s2 = new String("Test");
When you now compare this Strings with the == operator it will return false, because the reference of this strings is now different (you created 2 unique String objects).
But if you use s1.equals(s2) it will return true as expected.
When you are using
String test1="testing1"
then it means you are storing only one copy of each distinct string value
but
String test=new String("testing");
gives you a new string object.
Consider your second assignment was:
String1 test1 = System.getenv("PATH");
Here, test1 is most probably also a reference to a String object, without using new().
You can assign references to already existing objects to new variables.
So where is the problem?
The problem is, you must not use sloppy wording like "test1 is a String object". It is not. It is a reference to a String object or null. That's all about it.

How many String objects will be created here?

String x = new String("xyz");
String y = "abc";
x = x + y;
How many String objects would get created in this code?
There will be at least four objects:
The interned string "xyz"
The copy of the interned "xyz" string
The interned string "abc"
The result of concatenating the two strings.
String x = new String("xyz");
There's one: "xyz" is an interned string.
There's two: new String("xyz").
String y = "abc";
There's three: "abc" is an interned string.
x = x + y;
There's four. Since strings are immutable a third string object must be created: new String("xyzabc").
It's possible there could be a fifth object, because the compiler could use a StringBuilder to perform string concatenation.
StringBuilder s = new StringBuilder(x);
s.append(y);
x = s.toString();
When the class is loaded, two String objects are created (most likely), one for each String literal. This is a one-time thing ...
Each time the code is run, two Strings are created:
The new String("xyz") creates a new String whose state is the same as the "xyz" literal.
The String concatenation x + y creates a second new String.
It should be noted that String y = "abc"; does NOT create a new String. Rather, it assigns the reference to an existing String to y. In fact, it is the reference to the String object for the literal that was created when the class was loaded.
Actually, if you drill down, there is going to be a char[] array created for each of the String objects created. And the String concatenation may involve the creation of a temporary StringBuilder object. Both of these are implementation details.
It is also possible that loading the class might not result in creation of new String objects. It is true that the String literals will be represented by String Objects in the string pool, however the exact process of how that happens is an implementation detail ... and it doesn't necessarily entail calling String.intern on a freshly created String object.
And yet another answer is that ZERO objects get created. That is just Java source code, and Java source code doesn't create objects unless you compile it and run it. (Tada!!)
x : as you are using the new keyword and constructor of the class String a String object is created
"abc": is a string literal and Java creates a String object whenever it encounters a string literal.
x : the concatenation of two strings is transformed to StringBuilder.append(X).append(Y).toString(), so another object is created here.

How String Literal Pool Works

String str = new String("Hello");
Normally I have read, in many articles available on internet, that two objects are created when we write the statement above. One String object is created on the heap and one string object is created on the Literal Pool. And the heap object is also referring the object created on Literal Pool. (Please correct this statement of mine if it is wrong.)
Please note that the above explanation is as per my understanding after reading some articles on internet.
So my question is.. Are there any ways available to stop creating the string object in literal pool. How it can be done?
[Please let me know about the best link for understanding of this Literal Pool, How is it implemented]
There's only ever be one string with the contents "Hello" in the literal pool. Any code which uses a string constant with the value "Hello" will share a reference to that object. So normally your statement would create one new String object each time it executed. That String constructor will (IIRC) create a copy of the underlying data from the string reference passed into it, so actually by the time the constructor has completed, the two objects will have no references in common. (That's an implementation detail, admittedly. It makes sense when the string reference you pass in is a view onto a larger char[] - one reason for calling this constructor is to avoid hanging onto a large char[] unnecessarily.)
The string pool is used to reduce the number of objects created due to constant string expressions in code. For example:
String a = "Hello";
String b = "He" + "llo";
String c = new String(a);
boolean ab = a == b; // Guaranteed to be true
boolean ac = a == c; // Guaranteed to be false
So a and b refer to the same string object (from the pool) but c refers to a different object.
If I understand your question correctly, you're asking how to initialize a string without placing it in the string literal pool. You can avoid this as long as you don't declare a string literal expression (don't declare a series of characters in double quotes.)
// these two strings will be placed in the string literal pool
String one = "one";
String two = new String("two");
// this third string will NOT be placed in the string literal pool
String three = new String(new char[] {'t', 'h', 'r', 'e', 'e'});

Categories