Win a copy of Design for the Mind this week in the Design 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
Pie
Posts: 12097
30
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