Discussion:
equals( )
(too old to reply)
Nahid
2005-01-24 18:51:47 UTC
Permalink
Does the "==" sign call the "equals( )" automatically?

Rational g = new Rational ( 3, 1 );
Rational h = new Rational ( 3, 1 );
if (g == e)
System.out.println ( "equal" );
else
System.out.println ( "not equal" );

what would be the output? I got "not equal" when i ran it on my code? is
that what we are supposed to get?
Shimin Li
2005-01-24 20:36:56 UTC
Permalink
Post by Nahid
Does the "==" sign call the "equals( )" automatically?
No, in this project you are not asking to do any operator overriding
Post by Nahid
Rational g = new Rational ( 3, 1 );
Rational h = new Rational ( 3, 1 );
if (g == e)
System.out.println ( "equal" );
else
System.out.println ( "not equal" );
what would be the output? I got "not equal" when i ran it on my code? is
that what we are supposed to get?
In this case, g and h are references of two different objects, and (g == h)
is to test if these two references refer to the same objects, so the result
should be false. By the specification of method equal, g.equal(h) should be
true here.

Shimin
Gohulan
2005-01-25 01:59:32 UTC
Permalink
Post by Shimin Li
Post by Nahid
Does the "==" sign call the "equals( )" automatically?
No, in this project you are not asking to do any operator overriding
Post by Nahid
Rational g = new Rational ( 3, 1 );
Rational h = new Rational ( 3, 1 );
if (g == e)
System.out.println ( "equal" );
else
System.out.println ( "not equal" );
what would be the output? I got "not equal" when i ran it on my code? is
that what we are supposed to get?
In this case, g and h are references of two different objects, and (g == h)
is to test if these two references refer to the same objects, so the result
should be false. By the specification of method equal, g.equal(h) should be
true here.
Shimin
"==" would return true if and only if the variables are referring to the
same memory location which in this case is NOT true.

Loading...