Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use getClass() to tell if an Object is numeric?

I want to dynamically tell if an Object in Java is numeric or not. My code is as the following:

if(obj.getClass().equals(Number.class)) {
    attributeTypeStr = "Numeric";
} else {
    attributeTypeStr = "Non-Numeric";
}

The Object obj can be any numeric types like Integer, Float, Double, etc. But when I tested it with Integer/Float/Double, the attributeTypeStr always returns "Non-Numeric". I guess that's because Integer/Float/Double are subclasses of Number but Number.class does not equal Double.class. I could use something like

if(obj.getClass().equals(Integer.class) || obj.getClass().equals(Float.class) || obj.getClass().equals(Double.class))

But it looks too verbose. So is there a simple way to do that?

like image 335
tonga Avatar asked Sep 12 '13 20:09

tonga


People also ask

How do you check if an object is an instance of integers?

You may use Class. isInstance() method - This method is the dynamic equivalent of the Java language instanceof operator.

What does getClass () do in Java?

The getClass() method of Writer Class in Java is used to get the parent Class of this Writer instance. This method does not accepts any parameter and returns the required Class details. Parameters: This method accepts does not accepts any parameter.

How to get the name of the class of an object?

The Java Object getClass () method returns the class name of the object. The getClass () method does not take any parameters. Class of obj1: class java.lang.Object Class of obj2: class java.lang.String Class of obj3: class java.util.ArrayList In the above example, we have used the getClass () method to get the name of the class.

How to determine the class of an object in Java?

In this example, we will learn to determine the class of an object in Java using the getClass () method, instanceof operator, and the isInstance () method. To understand this example, you should have the knowledge of the following Java programming topics:

How to check if the object is an object of test?

Here, we have used the isInstance () method of the class Class to check if the object obj is an object of the class Test. The isInstance () method works similarly to the instanceof operator.

How to check if a type is numeric or not?

Rather than rolling your own, the most reliable way to tell if an in-built type is numeric is probably to reference Microsoft.VisualBasic and call Information.IsNumeric (object value). The implementation handles a number of subtle cases such as char [] and HEX and OCT strings. This should be at the top! Assuming your input is a string...


4 Answers

Use instanceof:

if (obj instanceof Number)
like image 147
Óscar López Avatar answered Oct 06 '22 00:10

Óscar López


Use Class#isAssignableFrom(Class) which states

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

Doing

Integer obj = new Integer(2);       
System.out.println(Number.class.isAssignableFrom(obj.getClass()));

will therefore print true.

It might not be relevant to your question, but just fyi:

  • What is the difference between instanceof and Class.isAssignableFrom(…)?
like image 44
Sotirios Delimanolis Avatar answered Oct 05 '22 22:10

Sotirios Delimanolis


Use isInstance() method of Class

if(Number.class.isInstance(obj)){
}

According to Class#isInstance()

Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.

like image 45
Prabhaker A Avatar answered Oct 06 '22 00:10

Prabhaker A


Check its superclass. This works as these classes are direct superclasses:

if(obj.getClass().getSuperclass().equals(Number.class)){
    //logic
}

Or for non-direct, check Number.class.isAssignableFrom(obj.getClass()). For numeric, it shouldn't be very much of a problem as the existing Number subclasses are final. In gnerics, this may break due to the absence of type information.

like image 38
nanofarad Avatar answered Oct 06 '22 00:10

nanofarad