• 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic