更多 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());
}