Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
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: 12441
42
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!