java - equals method not returning expected o/p -
please solve doubt equals(). think equlas() checks content in following example should print true because content same of both t1 , t2, prints false. why?
public class test { public static void main(string args[]) { test1 t1 = new test1(10); test1 t2 = new test1(10); system.out.println(t1.equals(t2)); } } class test1 { int ; test1( int x){ = x ; } }
thanks in advance
you need override equals
in test1
class desired behavior. otherwise, class inherit equals
object
, determines if 2 references refer same object. here, have different instances, false
result.
quoting linked javadocs:
the equals method class object implements discriminating possible equivalence relation on objects; is, non-null reference values x , y, method returns true if , if x , y refer same object (x == y has value true).
typically test if other object of same class, compare individual field(s) equality.
Comments
Post a Comment