• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

equals method .. comparing objects

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
please buy my thing and then I'll have more money:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic