• 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

Confusion about wrapper classes.

 
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,



The programs prints the output as d.equals(e) and why not d==e,if the line above (line 6)print the values of d and e as 1.0?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
while camparing "d==e " , here you are comparing two different objects(address of two object, always different d and e) so it returns false.

d.equals(e) this returns true because you are comparing value of the objects. so it returns true.

hope you are clear now.
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But ,Why line 6 is giving me the values as 1.0 1.0

If Im printing the objects it should give me something like Double@hashcode, right.


And if you print the hashcode for both the 'd' and 'e'.They are same


 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vijay tidake wrote:But ,Why line 6 is giving me the values as 1.0 1.0

If Im printing the objects it should give me something like Double@hashcode, right.


And if you print the hashcode for both the 'd' and 'e'.They are same




Maybe because Double provide a default implementaion of method toString() that return the value...
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you print an object it's converting to a String, using the toString() method. The default version of that in the Object class does what you describe, but lots of classes override it to do something more useful. Double is one of those - it overrides it to produce the same as writing out the primitive value.
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you mean to say if im camparing d==e ,Im actually doing String==String.

This is what you want to say?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. If you are comparing d == e, you are checking if they a referring to the same object.

If you write:This is the same as:
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The '==' implementation you are using is inherited from the Object class as you have not ovveriden
the method. The Object class is implemented in a way which tell, whether two objects are referring to
same memory location or not. In you case, you have created two different objects, so they will never
true as their memory address are not same.

Hope this helps.
Arhaan
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arhaan Singhania wrote:The '==' implementation you are using is inherited from the Object class as you have not ovveriden
the method.


That's slightly misleading. The == operator is not inherited from Object, and cannot be overridden. It always checks if the two references refer to the same object.

The equals() method is inherited from Object, and in Object is defined to use ==, which is where the confusion has arisen, I think. The equals() method can be overridden.

 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:

Arhaan Singhania wrote:The '==' implementation you are using is inherited from the Object class as you have not ovveriden
the method.


That's slightly misleading. The == operator is not inherited from Object, and cannot be overridden. It always checks if the two references refer to the same object.

The equals() method is inherited from Object, and in Object is defined to use ==, which is where the confusion has arisen, I think. The equals() method can be overridden.



Exactly!!! what d and e are storing(bitwise) is not the value 1 but the reference where two Double objects are stored.It does not reflect anything about the contents of the objects.
And as Matthew has rightly pointed, when you display the two objects, the toSring() method is called, which has been inherited and overriden by the Double class, to display its contents.
so although your print statement prints 1.0 and 1.0,
the if(d==e) fails.

And again, the equals() method is similarly inherited and overriden by the Double class to check whether the content of two Double objects being compared is the same!!! so the if() condition returns true!!!

so if you had something like this




Hope it clears your doubt!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic