What about the instanceof operator? What about giving the two parameters the type of Number?
Before doing this sort of thing, be careful to check the inheritance tree for the Number class; will it accurately compare a Double and an Integer, or even worse, a BigDecimal and an Integer?
these are all known subclasses of Number.
Direct Known Subclasses:
AtomicInteger, AtomicLong, BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
So whether it is a BigDecimal and Integer, i will change the value to double both, then compare with each other.
thanks. i know this can be done this way too. My main concern is to check is there any other best approach to check whether this object is a Number or not.
by the way, which technique you prefer 1 or 2 and why ?
all such methods can cause problems with sub-classes
I agree with your approach. But i didn't understand what type of problems it can create with the subclasses.
Can you give a sample code to illustrate this ?
Amandeep Singh wrote:My main concern is to check is there any other best approach to check whether this object is a Number or not.
If I looked at your code, I would conclude that your main concern was to tell whether the values of two Number objects were equal. But if your main concern is really to check whether an object is a Number, then
is the answer. That's all you need. All of that other code you posted is superfluous.