Forums Register Login

Best way to check whether object is Number

+Pie Number of slices to send: Send
i have written this best example code, to check whether given object is number or not.


if you have any other best way than this to check, let me know. thanks
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
That combination of "if" and ?: looks very confusing. Will it even compile?
+Pie Number of slices to send: Send
Definitely it will compile. its ternary operator. it might look confusing.
+Pie Number of slices to send: Send
So it will . . . What a surprise. It is still awful style.

Decent style would have been

yesNumber = ((Number)oldValue).doubleValue() == ((Number)newValue).doubleValue();

Now try this enhancement, and see what happens

+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
I would prefer the version with instanceof as being easier to read, but as you have seen, all such methods can cause problems with sub-classes.
+Pie Number of slices to send: Send
 

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 ?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

Amandeep Singh wrote:

all such methods can cause problems with sub-classes

Can you give a sample code to illustrate this ?

+Pie Number of slices to send: Send
I want to know what exactly processed during the execution of this code



Particularly in "Number.class" what is happening here.

Even though my query is not related to this topic, clarify me regarding this issue.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
I have doubt in "Number.class" step Rob.

Return type of "Number.class" is Class. how and where the ".class" method is called.
+Pie Number of slices to send: Send
It's not a method, but a bit like a static field which allows you to find the corresponding Class<T> object.
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 22087 times.
Similar Threads
To Static or Not To Static...
AtomicBigDecimal class -- interesting find
Problem inserting a single quote (')
AtomicDouble class -- update
Is this a correct use of ConcurrentHashMap?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:49:09.