Java Integer.class 与 int.class
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22470985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integer.class vs int.class
提问by user3380123
What is the difference between Integer.class
, Integer.TYPE
and int.class
?
之间有什么区别Integer.class
,Integer.TYPE
和int.class
?
acc to me
接受我
Integer.class
is a reference of Integer (Wrapper) Class object- but what is then
int.class
asint
is not a class, it's a primitive type. And what doesInteger.TYPE
refer to?
Integer.class
是 Integer (Wrapper) Class 对象的引用- 但是
int.class
asint
不是一个类,它是一个原始类型。而Integer.TYPE
指的是什么?
采纳答案by Evgeniy Dorofeev
From java.lang.Class.isPrimitive
API
来自java.lang.Class.isPrimitive
API
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.
有九个预定义的 Class 对象来表示八种基本类型和 void。它们由 Java 虚拟机创建,并与它们所代表的原始类型具有相同的名称,即 boolean、byte、char、short、int、long、float 和 double。
These objects may only be accessed via the following public static final variables java.lang.Boolean.TYPE
, java.lang.Integer.TYPE
etc
这些对象仅能通过下列公共静态最终变量来访问java.lang.Boolean.TYPE
,java.lang.Integer.TYPE
等等
回答by TheLostMind
In plain terms :
简单来说:
int -- > Are primitives..for simple math operations. You cannot add them to a collection.
Integer --> Are objects in themselves.. are wrappers to ints. i.e, they can be used with collections (as they are objects). They are collected as normal objects by the GC.
int --> 是基元..用于简单的数学运算。您不能将它们添加到集合中。
整数 --> 本身就是对象.. 是整数的包装器。即,它们可以与集合一起使用(因为它们是对象)。它们被 GC 作为普通对象收集。
EDIT :
编辑 :
public static void main(String[] args) {
int i = 5;
System.out.println(int.class);
Integer i1 = new Integer(5);
System.out.println(Integer.TYPE);
}
O/P : int
int
So, basically, both return an int. Integer.TYPE just returns the primitive type of the Integer class. It is true for any wrapper class
所以,基本上,两者都返回一个整数。Integer.TYPE 只返回 Integer 类的原始类型。对于任何包装类都是如此
回答by Willem Van Onsem
Java handles primitive types versus class types in a schizophrenic way by defining two types for each primitive.
Java 通过为每个原始类型定义两种类型,以精神分裂的方式处理原始类型与类类型。
For instance int
is the primitive type and Integer
the class type. When you use generics, you are forced to use a non-primitive type so ArrayList<Integer>
is allowed but ArrayList<int>
not.
例如int
是原始类型和Integer
类类型。当您使用泛型时,您被迫使用非原始类型,因此ArrayList<Integer>
允许但ArrayList<int>
不允许。
Since you sometimes want to perform reflection, this duality results in two classes (how else can you inspect a method public int foo ();
).
由于您有时想要执行反射,因此这种二元性会导致两个类(您还能如何检查方法public int foo ();
)。
Say you have a class:
假设你有一个类:
public class Foo {
private Integer value;
public int value1 () {
return value;
}
public Integer value2 () {
return value;
}
}
The two methods will not always return the same value, since value2()
can return null
and value1()
will throw a runtime error.
这两个方法不会总是返回相同的值,因为value2()
可以返回null
并value1()
抛出运行时错误。
回答by yshavit
Integer.class
is, as you say, a reference to the Class
object for the Integer
type.
Integer.class
正如您所说,是Class
对该Integer
类型的对象的引用。
int.class
is, similarity, a reference to the Class
object for the int
type. You're right that this doesn't sound right; the primitives all have a Class
object as a special case. It's useful for reflection, if you want to tell the difference between foo(Integer value)
and foo(int value)
.
int.class
是,相似性,Class
对int
类型对象的引用。你说得对,这听起来不对;Class
作为特例,原语都有一个对象。如果您想区分foo(Integer value)
和之间的区别,它对于反射很有用foo(int value)
。
Integer.TYPE
(not Integer.type
, mind you) is just a shortcut for int.class
.
Integer.TYPE
(不是Integer.type
,请注意)只是int.class
.
You can get a sense of this with a simple program:
您可以通过一个简单的程序了解这一点:
public class IntClasses {
public static void main(String[] args) {
Class<Integer> a = int.class;
Class<Integer> b = Integer.TYPE;
Class<Integer> c = Integer.class;
System.out.println(System.identityHashCode(a));
System.out.println(System.identityHashCode(b));
System.out.println(System.identityHashCode(c));
}
}
Example output (it'll be different each time, but the first two will always be the same, and the third will virtually always be different):
示例输出(每次都会不同,但前两个总是相同的,第三个几乎总是不同的):
366712642
366712642
1829164700