File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B p.359: JVM point of view Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B p.359: JVM point of view" Watch "K&B p.359: JVM point of view" New topic

K&B p.359: JVM point of view

Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
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

Joined: Jul 03, 2001
Posts: 2120
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 ".

SCJP2. Please Indent your code using UBB Code
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Thanks a lot for your response, Jose!
I agree. Here's the link:
subject: K&B p.359: JVM point of view
jQuery in Action, 3rd edition