Java String Constant Pool - number of objects created

How many string objects will the following code create?

String s = "Mahendra" + "Singh" + "Dhoni";

Will it create 4 string objects in a string pool or will it create more? Please explain in detail.

PS I saw other examples, and they concern only two string objects.

EDIT: From the answers it is clear that in the above case only one object will be created. Thanks so much for some very good suggestions. Sorry to ask a stupid question, but can you tell in the following example how many objects will be created?

String s = "Mahendra";
s = s + "Singh" + "Dhoni";

2nd EDIT: Sorry, but I still have some doubts. I see in the comments that in the above case 3 objects will be created: Mahendra, SinghDhoni and MahendraSinhDhoni. My doubt should not be calculated from left to right. Am I missing something - is it a wise concept? Please, help.

In addition, in another case, if I twist this question, how will the number of objects change. I ask about this in detail to clear it for many of my colleagues. All help is appreciated.

String s = "Singh";
s = "Mahendra" + s + "Dhoni";
+4
source share
3 answers

If your code is:

public static void main(String[] args) {
    String s = "Mahendra" + "Singh" + "Dhoni";
    System.out.println(s);
}

Look at the byte code:

 public static void main(java.lang.String[]);
   descriptor: ([Ljava/lang/String;)V
   flags: ACC_PUBLIC, ACC_STATIC
   Code:
     stack=2, locals=2, args_size=1
        0: ldc           #16                 // String MahendraSinghDhoni --> This line
        2: astore_1
        3: getstatic     #18                 // Field java/lang/System.out:Ljav
/io/PrintStream;
        6: aload_1
        7: invokevirtual #24                 // Method java/io/PrintStream.prin
ln:(Ljava/lang/String;)V
       10: return

, . , String .

EDIT: :

"Singh" + "Dhoni" . , 3 String. "Mahendra" "SinghDhoni" , mahendraSinghDhoni .

+5

1 s . , bwt String object literal, .

+1

, . . :

1

. , ,

String concatenation = "a" + "b" + "c";

String concatenation = "abc";

1 .

2

StringBuilder StringBuffer. ,

String a;
String b;
String c;
String d;
String e;

// Initialization and use of a, b, c, d, e
String concat = a + b + c + d + e;

- java-:

String a;
String b;
String c;
String d;
String e;

// Initialization and use of a, b, c, d, e

String concat = new StringBuilder(a).append(b).append(c).append(d).append(e).toString();

6 : a, b, c, d, e , a + b + c + d + e

3

StringBuilder , ,

String a;
String b;
String c;


// Initialization and use of a, b, c
String concat = a + b + c;

-, java-

String a;
String b;
String c;


// Initialization and use of a, b, c
String concat = a.concat(b).concat(c);

java- -:

public String concat(String str) {
    int otherLen = str.length();
    if (otherLen == 0) {
        return this;
    }
    char buf[] = new char[count + otherLen];
    getChars(0, count, buf, 0);
    str.getChars(0, otherLen, buf, count);
    return new String(0, count + otherLen, buf);
}

concat, String. 5 : a, b, c, a + b a + b + c

Note. any other compiler can choose another internal optimization from source code to compiled code. What you need to know is that in the worst case you will have 1 line generation for each start line and 1 line for each concatenation (each +). So, if you have 3 lines combined with two concatenation operations, you will have a maximum of 5 lines.

+1
source

All Articles