Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 738
leat Avatar asked Mar 02 '16 07:03

leat


2 Answers

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 .

like image 93
Gergely Bacso Avatar answered Oct 19 '22 04:10

Gergely Bacso


Because there is implicit autoboxing since Java 5 of primitive types. For other types you need to use an other syntax to achieve your goal: search for Generics.

Try this:

private static void printClass(String text, final Class<? extends Person> klazz) {
    System.out.printf("%s: %s%s", text, klazz, "\n");
}
like image 30
Xvolks Avatar answered Oct 19 '22 05:10

Xvolks