Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[q] What's the result, why?

Status
Not open for further replies.

mushitou

MIS
Sep 11, 2002
1
0
0
CN
public class Value
{
private int i;
}

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)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top