wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes doubts onRuntime String comparison like java == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubts onRuntime String comparison like java == "ja"+"va"" Watch "doubts onRuntime String comparison like java == "ja"+"va"" New topic
Author

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

madhu v pe
Ranch Hand

Joined: Apr 21, 2007
Posts: 100
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

Joined: Oct 02, 2003
Posts: 11355
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
nice to learn something new


You down with OOP? Yeah you know me!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: doubts onRuntime String comparison like java == "ja"+"va"