A, B, or C?

Which one compiles?
Which has no exceptions?
Which works the right way?

A) isUniqueDealAs1.
B) isUniqueDealAs2.
C) Both.

(Java 1.4.2)

int _iNum;
int _iNum1;
int _iNum2;
String _strID;

public boolean isUniqueDealAs1 (FilterTestObject ftoR) {
   if (this._iNum == ftoR._iNum
         && this._iNum1 == ftoR._iNum1
         && this._iNum2 == ftoR._iNum2
         && this._strID.equals((String)ftoR._strID))
      return true;

   return false;
}

public boolean isUniqueDealAs2 (FilterTestObject ftoR) {
   if (this._iNum == ftoR._iNum
         && this._iNum1 == ftoR._iNum1
         && this._iNum2 == ftoR._iNum2
         && this._strID == ftoR._strID)
      return true;

   return false;
}

Oh the frustrations I had. 2 hours of debugging for one simple, simple, SIMPLE mistake.

3 thoughts on “A, B, or C?

  1. Both compile. Only difference is that in A you are actually testing to see if the strings are equal while in B you are testing if they refer to the same String object .

    So the right way would be A .

  2. kashif’s right. good thing i was taught that from the beginning and didnt have to go through your pain. I know that in .NET that == is equiv to .equals() method in Java

  3. I remember reading that somewhere. But one would expect that the “==” would be overloaded to test for equality.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve this: Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.