• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to check whether object is Number

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That combination of "if" and ?: looks very confusing. Will it even compile?
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely it will compile. its ternary operator. it might look confusing.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amandeep Singh wrote:
all such methods can cause problems with sub-classes
Can you give a sample code to illustrate this ?
 
Dinesh Jayram
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.isAssignableFrom
 
Dinesh Jayram
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have doubt in "Number.class" step Rob.

Return type of "Number.class" is Class. how and where the ".class" method is called.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not a method, but a bit like a static field which allows you to find the corresponding Class<T> object.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic