Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B p.359: JVM point of view

 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks a lot for your response, Jose!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic