File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes counting String objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "counting String objects" Watch "counting String objects" New topic
Author

counting String objects

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
How many String objects are created by the expression
�apple� + �orange� + �banana�
a. 4
b. 5
c. It depends on the compiler
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
5?


Meng Yi
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
d. 1
The compiler creates only one object. Surprised me too.
[ May 09, 2003: Message edited by: Marlene Miller ]
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Marlene
Ar...
The question is tricky and also you are tricky too........
I am interesting to know this answer and the counting String objects #2 answer,
Please explain if anybody read this message. I will see your message in counting String objects #2
thanks for your attention


Francis Siu
SCJP, MCDBA
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Siu. I am going to answer here instead of in the other one, so as not to get mixed up with Jose�s explanation.
I have noticed that people in this forum count objects. In two contexts, garbage collection and String creation. One wonders if the skill of counting objects is needed for the exam.
While practicing counting String objects, I looked at the class file and the byte codes to check my answers. I learned that some of my assumptions were wrong.
I posted my examples to see if other people would disagree. I want to know if I am doing something wrong.
1. At compile-time, the concatenation of adjacent String literals �one� + �two� + �three� produces only one string �onetwothree� that is stored in the class file.
Jose explained that a String object is created when the string is referenced for the first time.
2. The Java Programming Language says the compiler �uses a StringBuffer object to build strings from expressions, creating the final String only when necessary�. That is what I see in the byte codes and what I was trying to show in my examples.
Thank you for your interest Siu and for your colorful replies.
[ May 10, 2003: Message edited by: Marlene Miller ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

Hi Marlene,
You commented: One wonders if the skill of counting objects is needed for the exam.
I have done both the 1.2 exam and the 1.4 upgrade, and I did not have any questions in either of them that required me to count objects.
Which is not to say that such questions do not exist of course.
I did have four questions that required me to work out when a variable was available for garbage collection though.
And your answer surprised me. I would have picked answer "b": 5.
Always good to learn something new.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Shabbir Rahman
Greenhorn

Joined: Feb 18, 2002
Posts: 18
So this will create 4 String literals and 1 String object, isn't it ?


-----------------<br />IBM XML Developer <br />SCJP 1.4
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
"Counting objects" (*) will not be in the exam.
However knowing that string literals are interned is valuable to answer questions about operator == when the operands are strings.
(*) How many objects are created by a given expression.


SCJP2. Please Indent your code using UBB Code
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Andrew, thank you for your comments on the exam.
So this will create 4 String literals and 1 String object, isn't it ?

Shabbir, I used my editor to look at the class file in ASCII. The compiler stored the string �appleorangebanana� in the class file. It did not store the three strings �apple�, �orange� and �banana�. I assume, therefore, in this program, there is only one String literal.
Method java.lang.String m()
0 ldc #2 <String "appleorangebanana">
2 areturn
I also looked at the byte codes of the class file Test.class. A reference to an instance of class String representing a string literal is pushed from the runtime constant pool (whatever that is) onto the operand stack. I assume, therefore, only one object is created.
I am only learning this stuff about string literals. This is my best guess.
Thank you Jose.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: counting String objects
 
Similar Threads
No. of String Objects
garbage collection
number of objects created?
String Objects
containers