Reputation: 1059
Can anyone please explain what is the difference between below implementation of String-
1)
{
String comma=",";
return finalStr = "Hello"+comma+"Welcome"+comma+"to"+comma+"Stack"+comma+"overflow";
}
2)
{
return finalStr = "Hello,Welcome,to,Stack,overflow";
}
How many string object will be created in first (1) block, will there be only one string finalStr
which refer to memory location where Hello,Welcome,to,Stack,overflow
is stored or will it create multiple locations for each word and then once appended it will create a new memory location.
Upvotes: 1
Views: 92
Reputation: 7652
As far as I know, In first cast JVM continuously making instant variables to keep Strings like "Hello", "Welcome", etc...
Further more, every appending operation it needs another variable to save appended String for example "Hello," + "Welcome" ...
On the other hand, second case, it allocate once for String. Thanks.
Upvotes: 0
Reputation: 7422
use javap to check how the compiler is trying to optimize the code, i first scenario, a concatenation happen using the StringBuilder and finally toString is called
Code:
0: ldc #16 // String ,
2: astore_1
3: new #18 // class java/lang/StringBuilder
6: dup
7: ldc #20 // String Hello
9: invokespecial #22 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
12: aload_1
13: invokevirtual #25 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
16: ldc #29 // String Welcome
18: invokevirtual #25 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
21: aload_1
22: invokevirtual #25 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
25: ldc #31 // String to
27: invokevirtual #25 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
30: aload_1
31: invokevirtual #25 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
34: ldc #33 // String Stack
36: invokevirtual #25 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
39: aload_1
40: invokevirtual #25 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
43: ldc #35 // String overflow
45: invokevirtual #25 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
48: invokevirtual #37 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
51: astore_2
Scenario 2
52: ldc #41 // String Hello,Welcome,to,Stack,overflow
54: astore_2
55: return
Upvotes: 0
Reputation: 32478
In both cases, only one String object will be created for each. Since, compiler is smart enough for understand the concatenation in compile time. These are string literals, they will be evaluated at compile time and only one string will be created for each cases.
As per JLS
A long string literal can always be broken up into shorter pieces and written as a (possibly parenthesized) expression using the string concatenation operator + [...] Moreover, a string literal always refers to the same instance of class String.
- Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
- Strings computed by concatenation at run-time are newly created and therefore distinct.
Upvotes: 3
Reputation: 6203
Take a look at this or this answer. The compiler will optimize some things for you. So stick with the most readable solution.
Similar questions have been asked several times, you will find some useful further information about your question if you read those answers.
Upvotes: 0