Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

String concatenation

 
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic