• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in equals method....

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Equl
{
public static void main(String[] s)
{
Integer i=new Integer(78);
Long l=new Long(78);
Double d=new Double(78);
if(i.equals(l))
System.out.println("1");
if(l.equals(d))
System.out.println("2");
if(d.equals(i))
System.out.println("3");
if(i.equals(78))
System.out.println("4");
}
}

output is:4

why it is giving 4 as output.
wrapper classes are peers so first 3 methods nt executed.
equals method is used to test whether two object are meaningfully equal or not.
but instead of object here we are passing 78 also it is not showing any problem...

why?
[ November 04, 2008: Message edited by: Ganeshkumar cheekati ]
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't understand what you are saying but the first three equals calls are returning false as equals method in wrapper classes, return true only if the object passed is of the same type as the object on which it was called and have the same value.

So a call like this

new Integer(48).equals(new Double(48))

will return false

and a call like this

new Integer(48).equals(new Integer(55))

will also return false

but a call like this one

new Integer(48).equals(new Integer(48))

will return true...
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if(i.equals(78))
System.out.println("4");
}

we have to pass reference variable which is pointing to an object in to an equals method.

but instead of that we are passing 78.

is it converted in to an object because of autoboxing feature?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it converts 78 to Integer(78) and then to Object i.e. boxing and then widening...
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so we should careful in case of wrapper classes because boxing and unboxing is happening automatically...

thanks a lot ankit..
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand what you saying here, but do not confuse with the following code.



Wrapper class is not the same with the primitive type, and Object.equal() method Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references:

It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi henry.
public class Equal
{
public static void main(String[] s)
{
int i=78;
long l= 78;
double d= 78;
if(i== l)
System.out.println("1");
if(l==d)
System.out.println("2");
if(d==i)
System.out.println("3");
if(i==78)
System.out.println("4");

}
}

program is giving output as

1
2
3
4

== operator looks for bit pattern in case of objects and value in the variable in case of primitives.here primitives are of different types.
why i got this output?

can anyone explain?
[ November 05, 2008: Message edited by: Ganeshkumar cheekati ]
 
Henry Zhi Lin
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ganeshkumar cheekati:


== operator looks for bit pattern in case of objects and value in the variable in case of primitives.here primitives are of different types.
why i got this output?

[ November 05, 2008: Message edited by: Ganeshkumar cheekati ]



As youself has answered the question.== operator looks for bit pattern. They are primitives and their bit vales are same, since the literal 78 assigned to all the variables.
 
reply
    Bookmark Topic Watch Topic
  • New Topic