2008-10-16 9 views

Odpowiedz

1

Ta metoda będzie również sprawdzić, czy jest to owinięcie z prymitywnego rodzaju, a także:

/** 
* Checks first whether it is primitive and then whether it's wrapper is a primitive wrapper. Returns true 
* if either is true 
* 
* @param c 
* @return whether it's a primitive type itself or it's a wrapper for a primitive type 
*/ 
public static boolean isPrimitive(Class c) { 
    if (c.isPrimitive()) { 
    return true; 
    } else if (c == Byte.class 
      || c == Short.class 
      || c == Integer.class 
      || c == Long.class 
      || c == Float.class 
      || c == Double.class 
      || c == Boolean.class 
      || c == Character.class) { 
    return true; 
    } else { 
    return false; 
    } 
+0

Użyj Number.class.isAssignableFrom (c) zamiast sprawdzać równość ze wszystkimi podtypami Number –

+0

@ Digitillusion Zawiera również typy nieprzechodzące, takie jak 'BigInteger', który jest' Number' też – Kapep

+0

'return c.isPrimitive() | | c.getSuperclass() == Numer.class || c == Boolean.class || c == Character.class; 'jest łatwiejszym rozwiązaniem –

Powiązane problemy