更多 Java 基礎(chǔ)知識方面的文章,請參見文集《Java 基礎(chǔ)知識》
Integer.class VS int.class
相同點:都會得到 Class<Integer>
不同點:
-
Integer是 Object Type 對象類型,int是 Primitive Type 原始類型 -
Integer有成員變量,有成員方法,int無成員變量,無成員方法 -
Integer:a reference to an int primitive -
int:a literal numerical value
示例:
public static void main(String[] args) {
Integer i1 = 123;
int i2 = 123;
Class<Integer> c1 = Integer.class;
Class<Integer> c2 = int.class;
// False
System.out.println(c1 == c2);
// False
System.out.println(c1.isPrimitive());
// True
System.out.println(c2.isPrimitive());
}
Integer.TYPE
得到 Class<Integer>:The class instance representing the primitive type int。
因此:
-
Integer.class與int.class不同 -
Integer.TYPE與int.class相同
示例:
public static void main(String[] args) {
Integer i1 = 123;
int i2 = 123;
Class<Integer> c1 = Integer.TYPE;
Class<Integer> c2 = int.class;
// True
System.out.println(c1 == c2);
// True
System.out.println(c1.isPrimitive());
// True
System.out.println(c2.isPrimitive());
}