Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

== and equal

 
Ash sav
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic