• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

equals method .. comparing objects  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Program 1

public class Equals{
public static void main(String[] args) {
Object n1 = new Object();
Object n2 = new Object();

System.out.println(n1.equals(n2));


}
}

result :false

Program 2
public class Equals{
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);

System.out.println(n1.equals(n2));

}
}

result : true



Could someone please help me out to understand why the results are different
in two of the above programs

and also I am having difficulty understanding difference between == and equals method when comparing objects. please help me out ...
[ July 29, 2006: Message edited by: Chris Wox ]
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The .equals method in Object simply performs a == comparision between object references.

Since n1 and n2 are references to different objects, the == comparison returns false.

The Integer class overrides equals and perform a comparison of the values stored in the Integer objects.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is very simple remember two things...
1) equals method checks the state.
2) == checks the references. it only returns true when the references have been made equal like a=b then a==b will give true.

3) equals method in object class only returns true when the two references have been made equal like a.equals(b) true only if a=b;

so in your first example you call equals on two objects which are instances of Object calss. the equals method in Object class only returns true when the references have been made equal. so false.
in second one Integer class overrides the equals method and according to that it checks the state of the object that is 42...so true.


i hope you get it....
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Program 2
public class Equals{
public static void main(String[] args) {
Integer n1 = new Integer(128);
Integer n2 = new Integer(128);

System.out.println(n1.equals(n2));

}
}

result : false

You need to understand when these wrapper objects are really equal!

In order to save memory two instances of the following wrapper objects will be always be == when their primitive values are the same:
Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
short and Integer from -128 to 127
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!