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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:57:07  来源:igfitidea点击:

Integer.class vs int.class

javareflectionprimitive

提问by user3380123

What is the difference between Integer.class, Integer.TYPEand int.class?

之间有什么区别Integer.classInteger.TYPEint.class

acc to me

接受我

  1. Integer.classis a reference of Integer (Wrapper) Class object
  2. but what is then int.classas intis not a class, it's a primitive type. And what does Integer.TYPErefer to?
  1. Integer.class是 Integer (Wrapper) Class 对象的引用
  2. 但是int.classasint不是一个类,它是一个原始类型。而Integer.TYPE指的是什么?

采纳答案by Evgeniy Dorofeev

From java.lang.Class.isPrimitiveAPI

来自java.lang.Class.isPrimitiveAPI

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.TYPEetc

这些对象仅能通过下列公共静态最终变量来访问java.lang.Boolean.TYPEjava.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 intis the primitive type and Integerthe 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 nulland value1()will throw a runtime error.

这两个方法不会总是返回相同的值,因为value2()可以返回nullvalue1()抛出运行时错误。

回答by yshavit

Integer.classis, as you say, a reference to the Classobject for the Integertype.

Integer.class正如您所说,是Class对该Integer类型的对象的引用。

int.classis, similarity, a reference to the Classobject for the inttype. You're right that this doesn't sound right; the primitives all have a Classobject 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是,相似性,Classint类型对象的引用。你说得对,这听起来不对;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