go to previous page   go to home page   go to next page highlighting

Answer:

BigInteger A = new BigInteger( "18" );
BigInteger B = new BigInteger( "18" );

if ( A == B )
  System.out.println("Trick Question");
else
  System.out.println("Didn't fool me");

if ( A.equals( B ) )
  System.out.println("Totally Expected");
else
  System.out.println("Reference vs Object Confusion");

Didn't fool me
Totally Expected

equals()

BigIntegers are objects, so == and equals() work as with all objects.

If two BigIntegers hold the same value (but are separate objects) then equals() evaluates to true.

However, if two BigIntegers hold the same value (but are separate objects) then the references (pointers) to the objects are different and == evaluates to false.

If myCheckingAccount holds 145 dollars and yourCheckingAccount holds 145 dollars, then
myCheckingAccount.equals( yourCheckingAccount ) is true.

However, myCheckingAccount == yourCheckingAccount is false because they are different accounts.

Your account is a different object than mine.

Remember that == looks only at what is in a reference variable and does not follow references to their objects. Since these are different objects, the references in each reference variable are different.


QUESTION 16:

What value do you imagine is returned by

myCheckingAccount.compareTo( yourCheckingAccount )

assuming both hold 145 dollars?


go to previous page   go to home page   go to next page