• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

K&B p.359: JVM point of view

 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, this topic has already been discussed in depth, there are 8 objects created up to the SOP line:

Here's a compilation listing. Can somebody tell me exactly on which lines new String objects are created?

I had to truncate some of the comments to be able to post...
[ October 09, 2003: Message edited by: Vad Fogel ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You asked for it, here you got it. That is JavaRanch.
0: ldc #2; //String spring
When the JVM finds the first use of an entry in the constant pool of the class, that corresponds to a string literal; it creates a String object and interns it. One string object s1 = "spring ".
14: ldc #6; //String summer
The same happens here. Two string objects "summer ".
19: invokevirtual #7; //Method java/lang/StringBuffer.toString:
The string buffer appends both strings and is converted to a new string object s2 = "spring summer ".
24: ldc #8; //String fall
Four strings "fall ".
26: invokevirtual #9; //Method java/lang/String.concat:
Five string objects "spring fall ". Though this one is eligible for gc. it is still created.
32: invokevirtual #9; //Method java/lang/String.concat:
Six string objects "spring summer spring ". This is neither pointed to by a reference.
47: ldc #10; //String winter
Seven "winter ".
52: invokevirtual #7; //Method java/lang/StringBuffer.toString
Eight s1 = "spring winter ".
70: ldc #12; //String
Nine " ".
79: invokevirtual #7; //Method java/lang/StringBuffer.toString
Finally ten strings were created "spring winter spring summer ".
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your response, Jose!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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