• 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  RSS feed

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
2) Given the following code,
Float f = new Float(0.9F);
Float g = new Float(0.9F);
Double d = new Double(0.9);
Which of the following conditions are valid and will return true?
a. (f==g)
b. (g==d)
c. (f.equals(new Float(0.9F))
d. (f.equals(d))
e. (f.equals(g))
The correct answer is c and e, but why not d???
Tnaks in advance.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is beca's equals() method of Object class is overridden in Float class and it checks whether the augument passed is instanceof Float.
If not, equals() returns false without comparing values wrapped.
Try to look into source code of Float.equals().
I hope it will make it more clear.

Originally posted by Jordi Marqu�s:
Hi!
2) Given the following code,
Float f = new Float(0.9F);
Float g = new Float(0.9F);
Double d = new Double(0.9);
Which of the following conditions are valid and will return true?
a. (f==g)
b. (g==d)
c. (f.equals(new Float(0.9F))
d. (f.equals(d))
e. (f.equals(g))
The correct answer is c and e, but why not d???
Tnaks in advance.


 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
d is false because u cannot use equals() to compare 2 non realted classes. but if u do, there is no error it simply returns false.
hope that helps
kavita desai
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still can't understand why
f == g; is false, but
f.equals(g); is true.
Could someone please explain?
Is it that == tests if the references denote the same object, unless they are primitives, then it tests if they hold the same value? Whereas .equals does the same except for Wrappers and Strings where it tests if they hold the same value?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siobhan,
this is possible because the equals() is overidden in all classes (so also the wrapper ones like Float, Double, Integer, and so on), but not in Object and StringBuffer, where equals() has the same effect as ==.
I hope this helps
Eddy
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
QUOTE
-------------------------------------
I still can't understand why
f == g; is false, but
f.equals(g); is true.
Could someone please explain?
Is it that == tests if the references denote the same object, unless they are primitives, then it tests if they hold the same value? Whereas .equals does the same except for Wrappers and Strings where it tests if they hold the same value?
------------------------------------
My observations..
1. == always checks for object reference equality.

2. that is it will check whether both objects refer to the same address.
3. Only in case of primitives it checks the values, but we will ignore that for our current discussion.
4. equals() also does the same thing; except for the classes that had overridden equals().
5. that is, equals also will check for object reference like == in all classes except STRING & WRAPPER classes.
6. In these two classes equals() can be used to check the contents. Such comparison is called as DEEP COMPARISON against the object reference comparison done by == which is known as SHALLOW COMPARISON.
7. In the case of wrapper class when equals() is applied,
first it will check whether the parameter passed is an object.
second it will check whether the objects being compared belong to the same class.
third it will look for the contents.
If all the three steps are satisfied then it will report true else false.
Now in this context u try to analyse the problem under reference.
when an object of double is compared with an object of float., the test fails in step 2. Hence the result.
HTH & u r clear.
Any comments...Welcome.
tvs sundaram
 
Siobhan Murphy
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tvs and Eddy. I think I have it now.
Best wishes
Siobh�n
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!