• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Confusion in Strings

 
Greenhorn
Posts: 10
2
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a bit confused for one concept in String.Suppose I have below code:



How many objects are created above.I thought two objects but since first string "abc" will go into string pool,so z will just point to this string and no new object will get created.Can anyone explain it more clearly.
 
Sheriff
Posts: 4981
319
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variations of this question have been asked many many times already on this forum. Use the Search Tool to find them.

My initial response to a question like this though is "who cares!". In nearly a decade of programming for a living, and plenty more years programming for a hobby, I have never needed to know. So it's fairly unlikely you do either. My advice is to forget about it and move on.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bhupinder Verma wrote:Can anyone explain it more clearly.


I'm with Tim: DontSweatIt. However, if you want a bit more background, you could try the CachedObjects page. There are also some others on this site that explain the String pool in more detail.

Winston
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with both of your points but this is typical interview question and mostly asked here. Even this question is asked by interviewer to me almost in every interview i faced.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bhupinder

For more info. see the section "Storage of Strings - The String Literal Pool" here:
http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html
 
Bhupinder Verma
Greenhorn
Posts: 10
2
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all.Got the concept now.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:Agree with both of your points but this is typical interview question...


Yeah, but IMO it's a bad one, because it makes people think about where objects are and how many they're creating; which is precisely what Java was intending to avoid. I have the same view about a lot of stuff in the SCJP exam.

However, the world is what it is; and there may still be team leaders around who worry about this sort of stuff.

Winston
 
We don't have time to be charming! Quick, read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic