Meaningless Drivel is fun!
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
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

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

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11881

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!
I agree. Here's the link:
subject: doubts onRuntime String comparison like java == "ja"+"va"
It's not a secret anymore!