How many String .. objects?

My friend and I were discussing on Strings, and we were stuck with this:

String str = "ObjectOne"+"ObjectTwo"; 

He says that only three objects will be created, and I will say that one object will be created.

Its logic is for three objects: one for "ObjectOne" and one for "ObjectTwo", and the third is a concatenated version of two String objects.

My logic of a single object at compile time as string objects will be combined in byte code as:

 String str = "ObjectOneObjectTwo"; 

And at runtime, only one object will be created this way. What is the truth.

+6
source share
6 answers

If you write (literals or constants)

 String str = "ObjectOne"+"ObjectTwo"; 

which is equivalent

 String str = "ObjectOneObjectTwo"; // compiler optimize it so one Object 
+11
source

You can find it yourself using the javap tool to parse the code to find out what the compiler made of it. Suppose you have this example:

 public class Example { public static void main(String[] args) { String s = "ObjectOne" + "ObjectTwo"; System.out.println(s); } } 

Compile it, then parse it with javap -c Example . Result:

 Compiled from "Example.java" public class Example { public Example(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: ldc #2 // String ObjectOneObjectTwo 2: astore_1 3: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 6: aload_1 7: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 10: return } 

As you can see, there is only one String object that contains "ObjectOneObjectTwo". So, really, the compiler does concatenation for you at compile time.

+7
source

You can easily verify this:

  • Compile the following program:

     public static void main(String[] args) { String str = "ObjectOne"+"ObjectTwo"; System.out.println(str); } 
  • Inspect the bytecode emitted by the compiler:

     javap.exe -v Test.class 

For the main method, this prints:

  public static void main(java.lang.String[]); flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=2, args_size=1 0: ldc #16 // String ObjectOneObjectTwo 2: astore_1 3: getstatic #18 // Field java/lang/System.out:Ljava/io/PrintStream; 6: aload_1 7: invokevirtual #24 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 10: return LineNumberTable: line 6: 0 line 7: 3 line 8: 10 LocalVariableTable: Start Length Slot Name Signature 0 11 0 args [Ljava/lang/String; 3 8 1 str Ljava/lang/String; } 

As you can see, the program uses the ldc instruction to refer to a single, already loaded instance of the line (which is loaded when Test.class). Therefore, at run time this line does not create a new object.

A compiler is required to perform this optimization using the Java language specification:

A String object has just been created (ยง12.5) if the expression is not an expression of a compile-time constant (ยง15.28).

+1
source
 String str1 = "ObjectOne"; String str2 = "ObjectTwo"; str1 = str1+str2; 

In the above case, three objects will be created.

But when you define how

 String str = "ObjectOne"+"ObjectTwo"; 

then only one object will be created. The compiler optimizes it.

0
source

The String object is immutable.

  String str = "ObjectOne"+"ObjectTwo"; is same as String str = "ObjectOneObjectTwo"; 

By immutable, we mean that the value stored in the String object cannot be changed. Then the next question that comes to our mind: โ€œIf String is immutable, then how can I change the contents of an object when I want?โ€ Well, more precisely, its not the same String object that reflects the changes you make. For internal changes, a new String object is created.

So, suppose you declare a String object:

  String myString = "Hello"; 

Then you want to add the "Guest" to the same Line. What are you doing?

 myString = myString + " Guest"; 

When you print the contents of myString, the output will be "Hello Guest". Although we used the same object (myString), a new object was created inside the process. So the mystic will refer to "Hello Guest." Hello link lost.

  String s1 = "hello"; //case 1 String s2 = "hello"; //case 2 

In case 1, literally s1 is created and stored in the pool. But in case 2, the letter s2 refers to s1; instead, it will not create a new one.

if (s1 == s2) System.out.println ("equal"); // Print Equals

  String s= "abc"; //initaly s refers to abc String s2 =s; //s2 also refers to abc s=s.concat("def"); // s refers to abcdef. s no longer refers to abc. 

enter image description here

0
source
 String str = "ObjectOne"+"ObjectTwo"; 

3 objects will be created as

1- "ObjectOne"

2- "ObjectTwo"

3- "ObjectOneObjectTwo"

using

 StringBuffer tmp = new StringBuffer("ObjectOne"); tmp.append("ObjectTwo"); str = tmp.toString(); 
-3
source

All Articles