Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

hii

 
aparna rudra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MyClass extends Object. Object's equals method returns true only if the two references are to the same object.
The Double class overrides the equals method so that it returns true if the values within each object being referred to are equal. In this case, they did not have to refer to the same object.
Check out the Java API for more on the topic.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!