简体   繁体   English

Java中的自动装箱

[英]Autoboxing in Java

How does following expression evaluated? 如何评估以下表达式?

Student class : 学生班:

public class Student
{
    private Integer id;
    // few fields here

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id=id;
    }

    //setters and getters
}

And in some method : 并在某些方法:

{
    int studentId;

    // few lines here

    if(studentId==student.getId())  // **1. what about auto-unboxing here? Would it compare correctly? I am not sure.**
    {
        //some operation here
    }
}

Yes, this will work it is equivalent to 是的,这将相当于

studentId==student.getId().intValue()  

as long student.id is not null . 只要student.id不为null

Yes this will work, but note! 是的,这将有效,但请注意!

If the Integer value id in Student is null, you will have a NullPointerException when evaluating 如果Student中的Integer值id为null,则在计算时将出现NullPointerException

studentId == student.getId();

Note also that autoboxing will have some performance cost, so you should only use it if you have to. 另请注意,自动装箱会产生一些性能成本,因此您只能在必要时使用它。

Read more here: http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html 在这里阅读更多内容: http//docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

Yes it will work fine. 是的,它会正常工作。 But it is usually not advisable to use wrapper class until there is not other go. 但是通常不建议使用包装器类,直到没有其他去。

The comparison 比较

studentId==student.getId()

will work, but will throw a NullPointerException if student is null . 将工作,但如果studentnull将抛出NullPointerException

As a rule autoboxing prefers primitives, ie it will convert an Integer to int where possible rather than the other way around. 通常,自动装箱更喜欢原语,即它会尽可能将Integer转换为int而不是相反。 Your example shows one good reason for this, since equality for reference objects is tricky. 您的示例显示了一个很好的理由,因为参考对象的相等性很棘手。 So it is possible for: 所以有可能:

studentId==student.getId().intValue()  

to be true but 是的,但是

new Integer(studentId)==student.getId()

to be false, since whilst they have the same value they're not the same object. 是假的,因为虽然它们具有相同的价值,但它们不是同一个对象。

Yes it will work, as it will convert right operand to corresponding numeric type, according to java language specification: 是的它会起作用,因为它会根据java语言规范将右操作数转换为相应的数字类型:

If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2). 如果等于运算符的操作数都是数字类型,或者一个是数字类型而另一个是可转换的(第5.1.8节)是数字类型,则对操作数执行二进制数字提升(第5.6.2节)。

corresponding paragraph in jls jls中的相应段落

So actually any of the operands can be of numeric type for java to autounbox the other one. 所以实际上任何操作数都可以是数字类型,java可以自动装配另一个操作数。

And then §5.1.8 says that conversions include unboxing conversion. 然后§5.1.8说转换包括拆箱转换。 corresponing paragraph in jls 在jls中对应段落

As per specifications, http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html 根据规范, http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

So when should you use autoboxing and unboxing? 那么什么时候应该使用自动装箱和拆箱? Use them only when there is an “impedance mismatch” between reference types and primitives, for example, when you have to put numerical values into a collection. 仅在引用类型和基元之间存在“阻抗不匹配”时才使用它们,例如,当您必须将数值放入集合时。 It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. 将自动装箱和拆箱用于科学计算或其他对性能敏感的数字代码是不合适的。 An Integer is not a substitute for an int; Integer不能替代int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it. autoboxing和unboxing模糊了原始类型和引用类型之间的区别,但它们并没有消除它。

The only advisable cases to use the wrapper classes ( Integer , etc.) are when you want to stick numeric values in a collection, or null is an acceptable value for your use-cases. 使用包装类( Integer等)的唯一可取的情况是,您希望在集合中粘贴数值,或者null是您的用例的可接受值。 That's it. 而已。

Side-effect include unwanted potential NullPointerException and decrease in performance. 副作用包括不需要的潜在NullPointerException和性能下降。

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

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