public boolean isPrimitive()
Determines if the specified Class
object represents a primitive type.java
There are nine predefined Class
objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean
, byte
, char
, short
, int
, long
, float
, and double
.app
These objects may only be accessed via the following public static final variables, and are the only Class
objects for which this method returns true
.this
Returns: true if and only if this class represents a primitive typespa
判斷是不是原始類型。code
Java的原始類型有八種:int, double, float, long, short, boolean, byte, char, void.ci
只有以這幾中類型調用時,返回true.get
示例代碼:it
package learn.classes; import org.junit.Test; public class Clazz { @Test public void isPrimitive() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException{ Class[] primitiveClasses={int.class,long.class,float.class,long.class, boolean.class, byte.class, char.class, short.class,double.class}; Class[] wrappedClasses={Integer.class,String.class}; for(Class clazz:primitiveClasses){ System.out.println(clazz.getSimpleName()+" is isPrimitive: "+clazz.isPrimitive()); } System.out.println("*****************************"); for(Class clazz:wrappedClasses){ System.out.println(clazz.getSimpleName()+" is isPrimitive: "+clazz.isPrimitive()); } } /* int is isPrimitive: true long is isPrimitive: true float is isPrimitive: true long is isPrimitive: true boolean is isPrimitive: true byte is isPrimitive: true char is isPrimitive: true short is isPrimitive: true double is isPrimitive: true ***************************** Integer is isPrimitive: false String is isPrimitive: false */ }