How many String objects does the new operator use

When we use the new operator to create a String object, I read that two objects are created by one object - this is a string constant pool, and the second in heap memory.

My question is here. We are using a new operator, so only one object should be created on the heap. Why then another object must be created in the String Constant pool. I know that Java stores a String object whenever we do not use a new operator to create a String. For instance,

String s = "abc" . 

In this case, only it will be created in the pool of String constants.

 String s2 = new String("abc") 

only one object must be created on the heap, and not in the constant pool.

Please explain why I am not right here.

+4
source share
7 answers

We are using a new operator, so only one object should be created on the heap.

Of course, the new operation creates only one object. But its parameter is a string literal that already represents the object. Each time you use a string literal, an object was created for it during class loading (unless the same literal was already used elsewhere). This is not skipped just because you are using this object as a parameter for the new String() operation.

And because of this, the operation new String() no longer needed and rarely used.

+10
source

See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28

String compilation time constant expressions are always interned to exchange unique instances using the String.intern method

That way when you write

 String s2 = new String("abc") 

The compilation time constant expression "abc" will be used.

+2
source

Two objects will be created, one object in the pool area and the other in the area without the pool, because as a parameter you use a new one as well as a string literal.

 String s = new String("abs"); 
+2
source

A string in java is immutable; after its creation, it cannot be changed. also, any string literal will be stored in the string pool for later use if the same literal is used again, for example:

 String name = "my name"; // one intern object is created in pool String otherName = "my name"; // the old intern object is reused System.out.println( name == otherName); // true , the same reference refer to same object 

two links refer to the same location in the pool.

 String name = new String("my name"); // one object is created, no string pool checking String otherName = new String("my name"); // other object is created, no string pool checking System.out.println( name == otherName); // false, we have 2 different object 

here we have two different String objects in memory, each of which has its own meaning.

see this article: http://www.javaranch.com/journal/200409/Journal200409.jsp#a1

+1
source
 String s2 = new String("abc") 

It will create only one object in the heap memory. We need to use the intern() method for java.lang.String explicitly to write to the string pool.

 String s = "def". 

Two objects will be created here. When you create a Java literal string notation, it automatically calls the intern() method to place this object in the string pool if it was not already in the pool.

+1
source

generate this line of code below will create 3 objects in the JVM

 String s2 = new String("abc") 
  • for "abc" in the String pool memory.
  • for the new operator, one object on the heap.
  • one more for s2 .
0
source

String s2 = new String("abc");
1 literal and 1 object will be created here.

With the new operator 1, a String object will be created on the heap as "abc", and s2 will refer to it, moreover, "abc" is also a string literal that is passed to the String Constructor, so it will be moved to the String Constant pool.

literal is considered as an object, so 2 objects will be created here.

-one
source

Source: https://habr.com/ru/post/1399609/


All Articles