• 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

How many String objects in this code?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all, I am preparing for the Sun Certification of Java and I have a question about the String class in Java.




my question is that in the above code how many objects are created after the line #3 executes...?

Please do provide your explanation as well. Thanks in advance.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't post the same question to multiple forums. I've deleted the other one.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It created two string objects. If s2 = "abc" , it would just create one object because of string literal pool
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

howie jao wrote:It created two string objects. If s2 = "abc" , it would just create one object because of string literal pool


But what about line 3?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one of the most famous String related question asked on JavaRanch. Do read this article for more information
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm not sure if the compiler can optimize this away or not.

Assuming it doesn't, I'd say five.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:i'm not sure if the compiler can optimize this away or not.

Assuming it doesn't, I'd say five.


5? Is it not 4? or each concatenation creates one string? that way it becomes 5.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that each concatenation creates a new one. I am not sure, and some older, wiser (or even a younger, wiser) person may come along and tell me I'm wrong, so don't quote me.
 
Ranch Hand
Posts: 72
Android Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I m too confused now.

(abc) (def) (" ") (abc ) (abc def) <- 5 (I think this is right )

OR

(abc) (def) (abc ) (abc def) <- 4
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaydeep Wagh wrote: Hello all, I am preparing for the Sun Certification of Java and I have a question about the String class in Java.




my question is that in the above code how many objects are created after the line #3 executes...?

Please do provide your explanation as well. Thanks in advance.



Creates 4 strings:
1. "abc"
2. "def"
3. " "
4. "abc def"
 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you guys use a de-compiler to see what does java compiler do to your code before generating the .class file.

Here is the de-compiled code for Hotspot. (the de-compiled code be different if the same code is compiled using different JVM like JRockit)




This clearly tells me that 4 String objects (abc, def, " " , abc def) and 1 String Builder (abc def) objects are created.
 
Jaydeep Wagh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Mohamed Sanaulla : Thanks buddy, the article was really good and helped me understand some of the underlying stuff of Strings in Java....!

@all : even I also considered that the no. of objects created at line #3 is 4 as replied by Nick Sher.
anyway thanks for the help.

I will consult all you experts in case I need any further help.
 
Happily living in the valley of the dried frogs with a few tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic