public class EqualsMethod2 {
public static void main(String[] args) {
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
}
}
========================
what's the result? why?
Thx!
Your source wouldn't even compile, because access to a private variable is not allowed from outside classes.
Anyway, v1 is not equal to v2, because you're comparing objects and not variables.
the object name in the jvm of v1 is Value@601bb1 and of v2 is Value@ba34f2. As you can see, thes "values" are not the same. May be different on your System. Use
System.out.println(v1 + " - " + v2);
to display these values inside of your method.
Modifying the compare method to
System.out.println(v1.i == v2.i);
returns true, because you are comparing identical values (i.e. 100)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.