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.
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 .
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
I remember reading that somewhere. But one would expect that the “==” would be overloaded to test for equality.