Reputation: 9569
I'm trying to compare two objects and both have the exact same values on each field.
System.out.println(basicTypes1);
[id=2013756475,
boolean2={true},
byte2={32},
short2={1,2},
int2={10,11},
float2={0.1,0.2},
double2={0.01,0.02},
number2={10,11},
char2={c,t},
string2={7c2cfc4e-5805-45ad-8687-4f2412a92e1d,d91b9609-39fb-4fe9-9f36-db33608970d1},
date2={Tue Jun 17 11:48:56 BST 2014,Tue Jun 17 11:48:56 BST 2014},
obj2={443f38f4-d5b4-41fb-aeeb-d086f4edc62a,d855d031-ad8d-4fce-b5e1-d959034a0b7c}]
System.out.println(basicTypes2);
[id=2013756475,
boolean2={true},
byte2={32},
short2={1,2},
int2={10,11},
float2={0.1,0.2},
double2={0.01,0.02},
number2={10,11},
char2={c,t},
string2={7c2cfc4e-5805-45ad-8687-4f2412a92e1d,d91b9609-39fb-4fe9-9f36-db33608970d1},
date2={Tue Jun 17 11:48:56 BST 2014,Tue Jun 17 11:48:56 BST 2014},
obj2={443f38f4-d5b4-41fb-aeeb-d086f4edc62a,d855d031-ad8d-4fce-b5e1-d959034a0b7c}]
Note that they both print the same result.
However, when I'm comparing the fields in each object the number2
and the string2
using .equals, the return value is false.
Here is a sample of the method where the values are being compared:
public boolean isEqual(Object basicTypes1, Object basicTypes2) {
boolean objectResult = true, fieldResult;
Class<?> leftClass = basicTypes1.getClass();
Class<?> rightClass = basicTypes2.getClass();
List<Field> fields = getAllFields(leftClass);
for (Field field : fields) {
field.setAccessible(true);
//the fields number2 and string2 enter in this if so I won't post the rest of them
if (TypeVerifier.isBasicType(field.getType())) { // handle primitives
fieldResult = leftFieldValue.equals(rightFieldValue); //comparing the above values, returns false, why?
if (!fieldResult) {
logger.error(getFieldError(leftClass, field, leftFieldValue, rightFieldValue));
}
}
objectResult = objectResult && fieldResult;
}
return obejctResult;
}
Is there any other way I should compare the Number[] number2
and the String[] string2
?
All the other fields return true in the same equals method (as expected).
number2={10,11} and string2={7c2cfc4e-5805-45ad-8687-4f2412a92e1d,d91b9609-39fb-4fe9-9f36-db33608970d1}
Thanks
Upvotes: 0
Views: 59
Reputation: 7042
you can try
for(int i=0;i<basicTypes1.string2.length;i++)
{
fieldResult=basicTypes1.string2[i].equals(basicTypes2.string2[i]);
}
and same things for numbers2.
Upvotes: 0
Reputation: 1008
equals() will compare the references of two objects, so except for primitives and the wrapper classes, you are comparing always by default two different objects.
The reason behind this mechanism is that the developer can override the equals method from the root class Object to decide when return a true for equality depending on the contents of the object. It's not possible, or reasonable, to create a equals() function for all the possible existent classes. The criteria in this case to create a equals method in Objects was check the references of both.
Number and String aren't primitives, so you are comparing references of different object, hence you get a false bool as return.
Check this link to get the full content: http://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html
Upvotes: 1