How many instances will be created in String str1 = new String ("abc")?

Have an interview question:

How many instances will be created in this expression:

String str1 = new String ("abc") 

And answer 2: str1 and "abc" .

Is it correct?

+6
source share
7 answers

Only one instance is created at runtime. When the class is loaded, the literal "abc" will be interned in the string pool , although technically speaking the JVM creates an instance of "abc" and stores it in the string pool . The expression new String("abc") creates an instance of String .

JLS 3.10.5 :

A string literal is a reference to an instance of the String class (section 4.3.1, section 4.3.3).

In addition, a string literal always refers to the same instance of the String class. This is because string literals, or, more generally, strings that are constant expression values ​​(Β§15.28), are β€œinterned” to exchange unique instances using the String.intern method.

Also read JLS 15.28

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

Recommended reading:

+7
source

No - only one instance is created on the heap. The literal "abc" goes to the pool. Compile and make javap and you will see only one new in the bytecode.

Consider this class -

 public class InstanceTest { public static void main(String... args){ String str1 =new String ("abc"); } } 

If we compile and do -

javap -c InstanceTest

We get -

 public class InstanceTest { public InstanceTest(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String...); Code: 0: new #2 // class java/lang/String 3: dup 4: ldc #3 // String abc 6: invokespecial #4 // Method java/lang/String."<init>":(Ljava/lang/String;)V 9: astore_1 10: return } 
0
source

str1 is a reference to a variable, not an object, and "abc" is a string literal, not an object

I think only one instance is created on the heap

0
source

the new keyword creates only one instance, and in your example you put the object reference in str1 (note: otherwise it will be deleted by the garbage collector).

0
source

You will create two instances of String. Using string constructors, rather than declaring them as literals, will always create different instances.

Just try the following code:

  String str = "abc"; String str2 = "abc"; String newString = new String(str); System.out.println("Are " + str + " and " + newString + " the same instance? " + (str == newString)); System.out.println("Are " + str + " and " + str2 + " the same instance? " + (str == str2)); 
0
source

Only one instance of the string will be created, since the java compiler has the intelligence to avoid unwanted creation of String objects. It will compile the file into a class file in which it tries to optimize the code as much as possible.

Read about string interning happening here http://en.wikipedia.org/wiki/String_interning

0
source

2 correctly, when using "abc" Java const pool creates an instance of "abc", use a new one ("abc"), it creates another instance of String on the heap.

0
source

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


All Articles