File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == and equal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== and equal" Watch "== and equal" New topic
Author

== and equal

Ash sav
Ranch Hand

Joined: Apr 14, 2001
Posts: 55
I was solving the exercise of KAM in chapter10. Please give me the answer in true/ false fashoin with some explantion.
a)If the references x and y denote two different objects, then the expression x.equals(y) is always false
b)If the references x and y denote two different objects, then the expression (x.hashCode()==y.hashCode()) is always false.
I know that in object class both == and equals work in similar way. So According to me answer of a) should be true. If I'm wrong then please correct me.
Thanks,
Ash
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Ash,
Yes you are correct, if we assume that the objects have not overridden the equals or hashcode methods.
a) True. Object equals method will always return false if both references don't point to the same object (memory location).
b) False. According to hashcode API: it is not required that if x and y are different objects that different integer values are returned.
Regards,
Manfred.
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
a) Not nessearly. You are allowed to override/overload equals, so it might have been changed to someting that compares something inseide the objects, instead of the reference-values. But as default: true.
b) Again this depends on you implementation. But this time it's even more to consider. hashCode() returns a value. If this is a value that's comparable by ==, it might return true, if they are identical in both objects.
ok?
/Mike
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
hi ash
have a look at this code below
class temp {
public static void main(String[] arg){
Object o1 = new Object();
Object o2 = new Object();
System.out.println(o1.equals(o2)); //false
System.out.println(o1==o2); //false
System.out.println(o1.hashCode()==o2.hashCode()); //false
}
}
as you can see all print false
yes you are right ... in Object class both == and equals work in similar way ... but these are two different objects which is the same as your first question ... and hence will return false
If i havent understood your doubt properly ... pls get back to this topic
hope that helps
Samith.P.Nambiar
-----------------------------------------
the harder you try the luckier you get
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
Hi Manfred,Nambiar
Try this:
Here u have two different objects. But
public class Stringequals
{
public static void main(String args[])
{
String a=new String("Java");
String b=new String("Java");
System.out.println("a== b is "+a==b);
System.out.println("a.equals(b) is "+a.equals(b));
}
}
Samith Nambiar
Ranch Hand

Joined: Mar 14, 2001
Posts: 147
here the equals() method returns true because this method has been overriden in the String class to compare the sequence of character
Samith.P.Nambiar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == and equal