File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes is getting me confused Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "is getting me confused " Watch "is getting me confused " New topic

is getting me confused

vinayak manda
Ranch Hand

Joined: Jul 27, 2004
Posts: 31
vinayak manda
Ranch Hand

Joined: Jul 27, 2004
Posts: 31
I'am sorry, I couldn't post the question before...

The question is when String s3 gets the value "EFG" and later s4 also gets the value "EFG", as the literal goes into the string pool,then s3 and s4 should point to the same reference.
Then why should (s3 == s4) return false.

Please clear this doubt.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
s3 gets a reference to a new String("EFG") and s4 gets a reference to another new String("EFG"). So s3 and s4 refer to different objects. Therefore s3 == s4 results in false.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Only String literals go into the pool by default. You can use the intern() method for the others to enforce them to get into the pool, too.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: is getting me confused
jQuery in Action, 3rd edition