Integer.class vs int.class

2019-01-17 06:18发布

问题:

What is the difference between Integer.class, Integer.TYPE and int.class?

acc to me

  1. Integer.class is a reference of Integer (Wrapper) Class object
  2. but what is then int.class as int is not a class, it's a primitive type. And what does Integer.TYPE refer to?

回答1:

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

These objects may only be accessed via the following public static final variables java.lang.Boolean.TYPE, java.lang.Integer.TYPE etc



回答2:

Integer.class is, as you say, a reference to the Class object for the Integer type.

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

Integer.TYPE (not Integer.type, mind you) is just a shortcut for 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


回答3:

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.

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



回答4:

Java handles primitive types versus class types in a schizophrenic way by defining two types for each primitive.

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.

Since you sometimes want to perform reflection, this duality results in two classes (how else can you inspect a method 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.