4

Following lines:

Class<Integer> clazzInteger = Integer.class;
Class<Integer> clazzInt = int.class;

Are valid and will compile/run even when:

if(clazzInt.equals(clazzInteger)) {
  System.out.println("clazzInt equals clazzInteger");
}else {
  System.out.println("clazzInt and clazzInteger are not equal");
}

Will print clazzInt and clazzInteger are not equal. But Class<int> clazzInt = int.class; do not work of course.

So why this analogy cannot be applied to array types?

Class<int[]> clazzIntArray = int[].class;
Class<Integer[]> clazzIntArray = int[].class;  // type mismatch: 
//cannot convert from Class<int[]> to Class<Integer[]>

But

Class<int[]> clazzIntArray = int[].class; // this is ok 

I'm now baffled why Class<Integer[]> clazzIntArray = int[].class is invalid? What Class<int[]> means? And why the analogy between array and non array types does not work?

Eliasz Kubala
  • 3,836
  • 1
  • 23
  • 28
plastique
  • 1,164
  • 2
  • 9
  • 19

3 Answers3

1

Under the hood, Autoboxing/Unboxing happens on individual elements inside the array. not with whole array type.

Java cannot magically convert whole primitive array to a Wrapper array. An individual element and an Array which consists of individual elements is the point here.

For ex : Array is a Basket and Fruit's inside the basket are elements (int's/Integer's)

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Autoboxing has nothing to do with it. The Java Language Specification specifies (in JLS 15.8.2) exactly what type T.class has:

  • If T is a reference type, T.class has type Class<T>
  • If T is a primitive type, T.class has type Class<wrapper class of T>

That's it. int.class has type Class<Integer> because the specification says so. int[].class has type Class<int[]> because the specification says so. Class<int[]> and Class<Integer[]> are not compatible types in Java.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • Thanks for answer, but `int.class` and 'Integer.class' are not equal. Does it means that `int.class` is somewhat subclass of `Integer.class` ? – plastique Aug 14 '15 at 06:09
  • @plastique: I didn't say `int.class` and `Integer.class` were equal (they are not). I said the expression `int.class` has type `Class` (so does the expression `Integer.class`). – newacct Aug 14 '15 at 09:26
  • Ok, thank for clarification. Still, there has to be some relationship between `int.class` and `Integer.class`. Both has type `Class` but their types are not equal. So sorry but i have to ask again is 'int.class' extending 'Integer.class' ? – plastique Aug 14 '15 at 09:55
  • @plastique: Sorry but your question doesn't make any sense. How can one value "extend" another one? – newacct Aug 14 '15 at 20:08
0

Autoboxing only works on single "instances" - an int can be autoboxed to an Integer and an Integer can be outboxed to an int. This is not true for arrays though. E.g., the statement int[] arr = new Integer[10]; will not compile. The same holds for manipulating these classes via reflection.

Mureinik
  • 297,002
  • 52
  • 306
  • 350