Search code examples
javaprimitiveboxing

Does int.class autobox to Class<Integer>


I am sure my question doesn't make sense, but it's because I don't know what I am seeing or how to describe it...

The following code compiles fine, but it shouldn't because int is not the same type as Integer. Shouldn't this give a compiler error? And if the compiler expects the type of Class<Integer> how at runtime does it get resolved to Class<int>? Is this some magic where the compiler lets it go on primitives? And if the compiler relaxes validation on primitives doesn't this lead to bugs where method writers expect the type to be the EXACT type Class<Integer> and instead is delivered Class<int>.

In short, why does this compile and produce the correct or wrong (depending on perspective) result at runtime.

public static void main(String[] args) {

    printClass("int     ", int.class);
    printClass("Integer ", Integer.class);
    System.out.printf("AreEqual", int.class == Integer.class);
}

private static void printClass(String text, final Class<Integer> klazz) {
    System.out.printf("%s: %s%s", text, klazz, "\n");
}

output:

int     : int
Integer : class java.lang.Integer
AreEqual: false

For the control group, this code does NOT COMPILE as I would expect

public static void main(String[] args) {

    printClass("Person  ", Person.class);
    printClass("Employee", Employee.class);
    System.out.printf("AreEqual: %s", Person.class == Employee.class);
}

private static void printClass(String text, final Class<Person> klazz) {
    System.out.printf("%s: %s%s", text, klazz, "\n");
}


public class Employee extends Person {
}
public class Person {
}

Errors:

Error:(8, 40) java: incompatible types: java.lang.Class<com.company.Employee> cannot be converted to java.lang.Class<com.company.Person>

Solution

  • What you observed is a direct result of the way generics handle primitives, it is not auto-boxing. To be consistent in cases where class-information will be used (like generics or reflection), primitive types in certain cases need to return with something when examining their type.

    When dereferencing anyprimitivetype.class, the TYPE field from the enclosing wrapper class will be returned. In your case it is:

     int.class -> public static final Class<Integer> TYPE
    

    You can find the full list here for all the primitive type plus void: primitives

    They are part of the language spec since 1.1 .