my dog learned polymorphism
The moose likes Beginning Java and the fly likes Doubt with == ( double equals ) method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt with == ( double equals ) method." Watch "Doubt with == ( double equals ) method." New topic

Doubt with == ( double equals ) method.

pradeep kumar rama

Joined: Apr 26, 2007
Posts: 3
I am trying to understand how the first output of the following piece of code returns "true"

As far as I know, the == operator compares two objects to determine if they are the same object in memory i.e. present in the same memory location. But here s1 and s5 point to different memory locations. Still how can the result be true ?

The Code I was refering to :

The result I got in the command prompt:

C:\Java Progs>javac

C:\Java Progs>java EqualsTest
== comparison : true
== comparison : true
Using equals method : true
Using equals method : true

C:\Java Progs>
marc weber

Joined: Aug 31, 2004
Posts: 11343

See our SCJP FAQ: Why do separate autoboxing conversions sometimes return the same reference?.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
I think the What is the String pool? FAQ would be more useful here.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

String literals behave differently than the rest of the world.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Doubt with == ( double equals ) method.
jQuery in Action, 3rd edition