Comparing two java objects on fly (data type not known)
I need to compare different data objects.
Can开发者_运维知识库 any one tell me how can i do this. I don't know what are the data types i will get priorly. If i need to use any util from apache commons then please give reference to it.
At present I am using .equals() for comparing equality of objects .It is working fine when I am comparing quality for two strings. If i am comparing java.sql.date data type then it is showing unequal even though both contains same values.
Can any one suggest me on this regard.
Thanks,
Narendrait totally depends on the equals()
method implementation of the object upon which you are calling the equals()
,
As you may know, the point of equals()
method is to compare object values as opposed to reference comparisons using the ==
or !=
operators.
In your example of java.sql.Date objects, the dates are probably not equal because certain parts of the time they represent are different - such as millisecond values - due to the level of accuracy.
In essence, it totally depends on the implementation of equals()
on each object; I'd also stress that you may want to clarify what exactly you define "equal" to mean - the same type at runtime for instance or containing the same public field names described by some reflection on the objects - and then work from there.
精彩评论