This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Strings

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In this particular code in the Kathy Sierra book it is give that eight objects in total are created but any opertaion on strings results in a new String. So in the system.out.print statement shouldn't there be two more objects created 1 which is s1+""(and soon to be lost) and then the final string object that is springwinterspringsummer(that too i guess will be lost) . Please let me know about this.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roohan,




1- "spring" (later on lost)
2- "springsummer " (s2 has reference of)
3- "fall " (lost, created when the compiler finds the String literal)
4- "springfall " (lost)
5- "springsummer spring" (lost)
6- "winter" (lost, created when the compiler finds the String literal)
7- "fallwinter" (s1 has reference of )
8- "springwinterspringsummer " (no reference lost)

Hope this helps you
Thanks and Regards,
cmbhatt
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roohan,
------------------------
String s1 = "spring";
String s2 = s1+"summer " ;
s1.concat("fall ");
s2.concat(s1);
s1+="winter";
System.out.println(s1+" "+s2)
-----------------------------
Here is what expect ...i am not considering lost or not(bec'ze its Gc issue). just how many objects created i am figuring out.

1. String s1 = "spring";
"spring" -- in pool(1)
2. String s2 = s1+"summer " ;
"summer" in pool (1)
"springsummer" in heap and pool(2)
3. s1.concat("fall ");
"fall" in pool(1)
"springfall " in heap and pool(2)
4. s2.concat(s1);
"springsummerspringfall " in heap and pool(2)
5. s1+="winter";
"winter" in pool(1)
"springfall winter" in heap and pool(2)

I found 12 Objects created both in heap and pool.
and total Strings combinations are 8.
 
Danger, 10,000 volts, very electic .... tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!