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>
Main.java:13: error: incompatible types: Class<Ideone.Employee> cannot be converted to Class<Ideone.Person> printClass("Employee", Employee.class);
andMain.java:14: error: incomparable types: Class<Ideone.Person> and Class<Ideone.Employee> System.out.printf("AreEqual: %s", Person.class == Employee.class);
– Lifegiving