This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
Joined: Mar 05, 2003
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 ]
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
"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
Joined: Mar 05, 2003
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.