简体   繁体   English

int.class autobox是否为Class <Integer>

[英]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 . 下面的代码编译得很好,但不应该因为intInteger类型不同。 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> ? 如果编译器期望Class<Integer>的类型在运行时如何解析为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> . 如果编译器放松了对原语的验证,这不会导致错误,方法编写者希望该类型是EXACT类型Class<Integer> ,而是传递Class<int>

In short, why does this compile and produce the correct or wrong (depending on perspective) result at runtime. 简而言之,为什么这会在运行时编译并生成correctwrong (取决于透视图)的结果。

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 对于控制组,此代码不像我期望的那样NOT COMPILE

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>

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. 取消引用anyprimitivetype.class ,将返回封闭包装类中的TYPE字段。 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 您可以在此处找到所有基本类型和void: primitives的完整列表

They are part of the language spec since 1.1 . 它们是自1.1以来语言规范的一部分。

Because there is implicit autoboxing since Java 5 of primitive types. 因为Java 5的原始类型存在隐式自动装箱。 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");
}

That's because this is the way java generics work. 那是因为这是java泛型工作的方式。 If you want to allow all classes that inherit from Person you may use: 如果要允许所有继承自Person的类,可以使用:

private static void printClass(String text, final Class<? extends Person> klazz) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM