• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

counting String objects

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many String objects are created by the expression
�apple� + �orange� + �banana�
a. 4
b. 5
c. It depends on the compiler
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
5?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d. 1
The compiler creates only one object. Surprised me too.
[ May 09, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this will create 4 String literals and 1 String object, isn't it ?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic