简体   繁体   中英

How to test if an object is primitive type or an array of primitive type in Java?

Here the function signature I would like to implement.

public boolean isBaseTypeOrArray(Object obj){~}

I want to return true only if obj is of one of the following types.

boolean or boolean[]
byte or byte[]
short or short[]
int or int[]
long or long[]
float or float[]
double or double[]
char or char[]
java.lang.String or String[]

For a lone value checking if it is instance of one of the wrapper classes (Integer, Float, ...) or String should work because of auto boxing but I do not know how to check for the array case. Any ideas?

  • To test if object is an array just getClass() and see if it isArray() .
  • To get type of elements array is declared to hold use getComponentType() on Class instance.
  • To test if some type is primitive you can use isPrimitive() .
  • If you want to check if type represents String just use equals(String.class) .

So to test if Object represents array of primitive type or array of strings you can use

public static boolean isBaseTypeOrArray(Object obj) {
    Class<?> c = obj.getClass();
    return  c.equals(String.class) 
         || c.equals(String[].class)
         || c.isArray() && c.getComponentType().isPrimitive();
}

Problem with above method is that it can't accept primitive type values because it expect Object obj which means that it enforces autoboxing of any primitive type value to its wrapper class. For instance if wa call it like isBaseTypeOrArray(1) primitive int 1 will be wrapped to Integer 1.

To accept arguments of pure primitive type (like int and not Integer ) we need overloaded versions of that method which would accept primitive types like boolean isBaseTypeOrArray(int obj) . Such methods can immediately return true as result because fact that they ware invoked means that primitive type value was passed as argument.

So to handle all primitive types we need to add below methods:

public static boolean isBaseTypeOrArray(boolean obj) {return true;}
public static boolean isBaseTypeOrArray(byte obj) {return true;}
public static boolean isBaseTypeOrArray(short obj) {return true;}
public static boolean isBaseTypeOrArray(char obj) {return true;}
public static boolean isBaseTypeOrArray(int obj) {return true;}
public static boolean isBaseTypeOrArray(long obj) {return true;}
public static boolean isBaseTypeOrArray(float obj) {return true;}
public static boolean isBaseTypeOrArray(double obj) {return true;}

I think, you should dynamically get and test a class description of your value:

if (obj.getClass().equals(Boolean.class)) {
    //...
}
//...
if (obj.getClass().equals(boolean[].class)) {
    //...
}
//...
if (obj.getClass().equals(String.class)) {
    //...
}

You can try something like this

public static void main(String[] args) {
    System.out.println(isBaseTypeOrArray(new int[5]));
}

public static boolean isBaseTypeOrArray(Object obj){
   return (obj.getClass().getComponentType().equals(int.class)||
           obj.getClass().getComponentType().equals(byte.class));
}

Simple solution

public static boolean isPrimitiveArray(Object obj) {
    return obj != null 
        && obj.getClass().isArray() 
        && obj.getClass().getComponentType() != null 
        && obj.getClass().getComponentType().isPrimitive();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM