• 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

loops

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here are the two questions which i am not able to get. they are from the mock exams taken from certification4career.com.
Double a=new Double(Double.NAN)
Double b= new Double(Double.NAN);
if (a.equals(b))
System.out.println("true);
System.out.println("false");
here the out put is true.
wht abt this question
class MyClass
{
int maxElements;

MyClass(int maxElements)
{
this.maxElements = maxElements;
}
}
public class Q19
{
public static void main(String[] args)
{
MyClass a = new MyClass(100);
MyClass b = new MyClass(100);
if(a.equals(b))
System.out.println("Objects have the same values");
else
System.out.println("Objects have different values");
}
}
the output is objects have different values.
why is this so. whether i declare it as static or not the ouput is same. why is this?
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because in the latter case, no equals() function had been written, so it defaults to ==. You might want to read up on when and how == differs from equals(), and when they are the same.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
In the second case you did not override the "public boolean equals(Object o)" method so in this case you use method wich was inherited from Object, but this method in Object class compare two objects using ==. So you have two references wich refer to two different objects and becase of it the answer is false.
In the first case a and b both represent Double.NaN and equals method returns true, even though Double.NaN == Double.NaN returns false.
[ April 06, 2004: Message edited by: Serghei Jelauc ]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The equals method is inherited from the Object class which is the parent of all classes in Java. The default activity performed by this method is to check the hanles of the specified objects using == operator. This is same as using if (a == b) in your case (which won't be equal).
For this purpose, you need to overide the equals method in order to provide your own implementation.
Regarding the first example that you provided, it seems that you are getting output of true. I think this should in fact be false because you are comparing two objects having value Double.NaN. But Double.NaN.equals(Double.NaN) will always be false. Please verify this.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thing we have to know is that equals() belongs to Object class�
Secondly all wrapper classes override Object.equals() now comes what does it mean to override equals() and what makes equals() return true or false�.
If Object.equals() if overridden the contents of the objects are compared the references are not compared , but both objects must be of same type. Applying this to your first question you can c Double being a wrapper class, the contents of the objects are only compared references are not�.therefore it returns true.
In your second question MyClass does not override Object.equals()�..
When Object.equals() is not overridden, references are compared the contents are not compared. Therefore objects a and b of MyClass refer to two different references so a.equals(b) returns false thought their contents are same�..i hope u got it�
To make you more confused lets c how can we override Object.equals() in MyClass

class MyClass
{
int maxElements;
MyClass(int maxElements)
{
this.maxElements = maxElements;
}
public int getValue(){
return maxElements;
}
public boolean equals(Object o){
if((o instanceof MyClass)&&(((MyClass)o).getValue()==this.maxElements)){
return true;
}
else{
return false;
}
}
}
public class Q19
{
public static void main(String[] args)
{
MyClass a = new MyClass(101);
MyClass b = new MyClass(101);
if(a.equals(b))
System.out.println("Objects have the same values");
else
System.out.println("Objects have different values");
}
}
(o instanceof MyClass) this is not necessary but to be sure that the objects being tested are of the same type this is suggested.
((MyClass)o).getValue() we are casting o to MyClass to avoid compiler error as Object class does not have get Value() method but MyClass does.
this.maxElements this returns the value of object from which the equals method was invoked��.
Now by combining all
(((MyClass)o).getValue()==this.maxElements)){
we r checking if the value if the object passes in equals() is equal to the value of the object on which equals() was invoked.
This returns Objects have the same values as now MyClass overrides Object.equals() and only the contents are compared not the references�.i hope I didn�t confuse you too much but as this is very vital for the exam u might just want to glance�..and if I am wrong anyone plz accurate me! Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic