• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Best way to check whether object is Number

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That combination of "if" and ?: looks very confusing. Will it even compile?
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Definitely it will compile. its ternary operator. it might look confusing.
 
Campbell Ritchie
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Marshal
Posts: 28264
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22789
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class.isAssignableFrom
 
Dinesh Jayram
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79475
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic