wood burning stoves 2.0*
The moose likes Java in General and the fly likes Best way to check whether object is Number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Best way to check whether object is Number" Watch "Best way to check whether object is Number" New topic
Author

Best way to check whether object is Number

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
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


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
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

Joined: Jul 17, 2008
Posts: 844
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

Joined: Oct 13, 2005
Posts: 38107
    
  22
That combination of "if" and ?: looks very confusing. Will it even compile?
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Definitely it will compile. its ternary operator. it might look confusing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
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

Joined: Jul 17, 2008
Posts: 844
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

Joined: Oct 13, 2005
Posts: 38107
    
  22
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

Joined: Jul 17, 2008
Posts: 844
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 13, 2005
Posts: 38107
    
  22
Amandeep Singh wrote:
all such methods can cause problems with sub-classes
Can you give a sample code to illustrate this ?
Dinesh Jayram
Greenhorn

Joined: Nov 26, 2008
Posts: 23
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

Joined: Oct 27, 2005
Posts: 19656
    
  18

Class.isAssignableFrom


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dinesh Jayram
Greenhorn

Joined: Nov 26, 2008
Posts: 23
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

Joined: Oct 13, 2005
Posts: 38107
    
  22
It's not a method, but a bit like a static field which allows you to find the corresponding Class<T> object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way to check whether object is Number
 
Similar Threads
Problem inserting a single quote (')
AtomicBigDecimal class -- interesting find
To Static or Not To Static...
AtomicDouble class -- update
Is this a correct use of ConcurrentHashMap?