aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String concatenation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String concatenation" Watch "String concatenation" New topic
Author

String concatenation

shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
hi all,
String str = "hello" + "world" is equivalent to new StringBuffer().append("hello").append("world").toString() where "hello" and "world" are appended to the StringBuffer object and a new string representation of it is returned.
My question is are 'hello' and 'world' created as literals in the string pool??? and so consequently three string objects(two in the pool and one in the heap) created??? and of course a StringBuffer object ...
i want to know how many objects are created in the string pool and in the heap for the above statement..
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
Hi,
Yes both "hello" and "world" are created as string literals in the literal pool. Also if you have expression like
String str = "hello" + "world";
This will create a new string literal "helloworld" in the literal pool. This is because constant expressions are computed at compile time and then treated as if they were literals. So if you have another line in your program
String str1 = "helloworld";
then str1 == str;

AM<BR> <A HREF="mailto:anshulmohan@rediffmail.com" rel="nofollow">anshulmohan@rediffmail.com</A>
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
You are exactly right that Java uses StringBuffer to concatenate for "+". So, "hello"+"world" creates "hello", "world", a StringBuffer that appends the two Strings, and the resulting String from the StringBuffer's toString() which would be "helloworld". But on most mock exams I see only 3 objects with the StringBuffer omitted. Can someone please shed some light on how we should approach the actual exam, include the StringBuffer or not?
Thank you.
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
You are exactly right that Java uses StringBuffer to concatenate for "+". So, "hello"+"world" creates "hello", "world", a StringBuffer that appends the two Strings, and the resulting String from the StringBuffer's toString() which would be "helloworld". But on most mock exams I see only 3 objects with the StringBuffer omitted. Can someone please shed some light on how we should approach the actual exam, include the StringBuffer or not?
Thank you.
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74
Cameron the I believe that StringBuffer implementation of concatenation would come into effect only if the the value is computed at runtime. In the above example since two constant string literals are being concatenated the value will be computed a compile time and it will be used at string literal. From JLS 3.10.5

Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
Strings computed at run time are newly created and therefore distinct.
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
hi all,
so should i conclude that the above statement creates 3 string literals viz. "hello","world" and "helloworld" in the pool and nothing in the heap ???
shanks.
shankar vembu
Ranch Hand

Joined: May 10, 2001
Posts: 309
hi,
anshul is right.
consider this code snippet:
public class test {
public static void main(String argv[]){
String temp = "hello";
String str = temp + "world";
String str1 = "helloworld";
System.out.println(str==str1);// false
}
}
here, StringBuffer is used while concatenating and so str!=str1.
new StringBuffer().append(temp).append("world").toString().
"hello","world" and "helloworld"(str1) are created in the pool.
a StringBufer is created in the heap(str) and also its String representation.
so, three in the pool and two in the heap for the above code snippet.i think i am right.
thanx to all,
shanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String concatenation
 
Similar Threads
Strings in Java
String Handling
RHE Qs
intersting question
String is created on pool?