• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion in Strings  RSS feed

 
Greenhorn
Posts: 8
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 934
4
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • 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: 8
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.Got the concept now.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!