getComponentType、isPrimitive方法说明

Class<?> getComponentType()方法是Class中的方法,可以返回表示数组类型的Class。 boolean isPrimitive()用来判断指定的Class类是否为一个基本类型。 例:

String [] arr = new String[10];
String str = "";
System.out.println(arr.getClass()); // 数组的String 类
System.out.println(str.getClass()); // 非数组的String 类
System.out.println(arr.getClass().getComponentType()); // String类
System.out.println(str.getClass().getComponentType()); // 得到null值,因为str不是数组
System.out.println(arr.getClass().getComponentType().isPrimitive()); // 显示false,因为String 不是基本数据类型

int [] arr = new int[10];
System.out.println(arr.getClass().getComponentType().isPrimitive()); // 显示true,因为int是基本数据类型
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章