• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

equals in 1.4 and 1.5

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer a = new Integer(10);
Double b = new Double(10.0);

System.out.println(a.equals(b));

false

my doubt is Is there any difference if I run in 1.4 1nd 1.5?
I thought equals means actually compare the object values rather than comparing their references.
explain me about wrapper objects.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anmita payal:
...I thought equals means actually compare the object values rather than comparing their references...


It's a little more than that.

If you check the API documentation (for either 1.4 or 1.5) for Integer's equals method, you will see that...

The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.


In your example, the argument is a Double, which is not an Integer object, and so the method returns false.
 
anmita payal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc for reply but can again can you explain this:

Double a = new Double(10.0);
Double b = new Double(10.0);

System.out.println(a.equals(b));

it'e true.

Means what I understood from API is It will return true if Objects are of same type.
I got one more doubt is here when we are aomparing Objects it is checking the values and not references....
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anmita payal:
...Means what I understood from API is It will return true if Objects are of same type.
I got one more doubt is here when we are aomparing Objects it is checking the values and not references....


For the wrapper classes, that's correct. The objects must be the same type and hold the same values. The references do not matter.

This is the kind of implementation we would expect for equals. But keep in mind that equals can be overridden differently in each class. And without any overriding, a class inherits the implementation in Object, which works more like a comparison of references.
 
anmita payal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc , now I got an idea.
Monday i am giving the exam. I hope I will do better.
 
The only thing that kept the leeches off of me was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic