This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubts onRuntime String comparison like java == "ja"+"va"

 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
any one can help on below code how it checks for equality?
Question 1


Q2) can anyone explain about comparison rules for String Buffer

Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals are a little tricky. The compiler is smart enough to resolve "ja" + "va" into "java", which it knows is the same as what the variable java is set to. So, it sets the references to point to the same spot in the string pool.

but it is NOT smart enough to know that "ja" + va, where va is a variable that can change, resolves to "ja" + "va", etc.

So, that will not be resolved until runtime, thus pointing to a different place.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice to learn something new
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic