应该在Java中避免自动装箱

前端之家收集整理的这篇文章主要介绍了应该在Java中避免自动装箱前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想询问是否应该在 Java中避免自动装箱.由于有一些方法需要一个原始类型“double”,而是传递一个“Double”对象作为参数,所以应该避免这种情况,因为编译器会打开你传递的对象并且可能很重?

解决方法

这是Java Notes在 autoboxing上说的:

Prefer primitive types

Use the primitive types where there is no need for objects for two
reasons.

@H_404_14@
  • Primitive types may be a lot faster than the corresponding wrapper
    types,and are never slower.
  • The immutability (can’t be changed after creation) of the wrapper
    types may make their use impossible.
  • There can be some unexpected behavior involving == (compare references) and .equals() (compare values). See the reference below for examples.
  • 猜你在找的Java相关文章