GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals() and == 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 "equals() and ==" Watch "equals() and ==" New topic
Author

equals() and ==

thanvi janu
Greenhorn

Joined: Nov 16, 2009
Posts: 29
this is thanvi, i have some doubts in equals() method

1. String a=new String();
String b=new String();
System.out.println((a == b) + " " + (a.equals(b)));
output was false true

2. Test t=new Test(); // user defined class
Test aa=new Test();
Test ab=new Test();
System.out.println((aa == ab) + " " + (aa.equals(ab)));
output was false false

3. Integer c=new Integer(10);
Integer d=new Integer(10);
System.out.println((c == d) + " " + (c.equals(d)));
output was false true

working with these three example i came to a conclusion that

Predefined class equals(): checks for the object
User defined class equals(): checks for the reference

4. Object o1 = new Object();
Object o2 = new Object();

System.out.println((o1 == o2) + " " + (o1.equals
(o2)));

I thought answer for 4 will be false and true because it is a Predefined class, but when i complied it the answer was false and false.

what is actually happening in example 4?
why in user defined class, equals() checks for reference instead of object?
Is my understand correct?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9395
    
    2

Object is not an immutable class...


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

thanvi janu wrote:
working with these three example i came to a conclusion that

Predefined class equals(): checks for the object
User defined class equals(): checks for the reference


Many of the Java core classes (String, Integer, etc.) override the equals() method, in order to provide a definition of equality based on the value of the object. Some don't, in which case they will inherit the equals() method from the super class -- and the Object class uses references to determine equality.

As for user defined classes, it would depend on if they overridden the equals() method. If they don't, then they would inherit the implementation from the Object class.

thanvi janu wrote:
I thought answer for 4 will be false and true because it is a Predefined class, but when i complied it the answer was false and false.

what is actually happening in example 4?
why in user defined class, equals() checks for reference instead of object?
Is my understand correct?


Since the Object class' equal() method is what is inherited when classes don't override equals, it is understandable that it behaves this way -- from your previous experiment.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
thanvi janu
Greenhorn

Joined: Nov 16, 2009
Posts: 29
tanx Henry
 
GeeCON Prague 2014
 
subject: equals() and ==