public boolean isPrimitive()
Determines if the specified Class
object represents a primitive type.
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
.
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
.
Returns: true if and only if this class represents a primitive type
判断是否是原始类型。
Java的原始类型有八种:int, double, float, long, short, boolean, byte, char, void.
只有以这几中类型调用时,返回true.
示例代码:
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
*/
}