File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt with == ( double equals ) method." Watch "Doubt with == ( double equals ) method." New topic
Author

Doubt with == ( double equals ) method.

pradeep kumar rama
Greenhorn

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 EqualsTest.java

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

C:\Java Progs>
marc weber
Sheriff

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
sscce.org
Joanne Neal
Rancher

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


Joanne
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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: http://aspose.com/file-tools
 
subject: Doubt with == ( double equals ) method.
 
Similar Threads
string
String question
== & equals()
difference between equals() and ==
(s1==s5) Vs s1==s5