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?
You may use Class. isInstance() method - This method is the dynamic equivalent of the Java language instanceof operator.
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.
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.
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:
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.
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...
Use instanceof
:
if (obj instanceof Number)
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With