java - Java 中应该避免自动装箱吗?

标签 java autoboxing

在某些情况下,方法需要基本类型 double 并且您将 Double 对象作为参数传递。

由于编译器对传递的对象进行拆箱,这会增加内存使用量还是降低性能?

最佳答案

这是 Java Notes 在 autoboxing 上所说的内容:

Prefer primitive types

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

  1. Primitive types may be a lot faster than the corresponding wrapper types, and are never slower.
  2. The immutability (can't be changed after creation) of the wrapper types may make their use impossible.
  3. There can be some unexpected behavior involving == (compare references) and .equals() (compare values). See the reference below for examples.

关于java - Java 中应该避免自动装箱吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7610664/

相关文章:

java - 如何通过 XSLT 在 Java 中合并 2 个 XML 流

java - 实现将盖子与正确尺寸的 jar 相匹配的算法

Java - 如何编写自己的异常来查找特定错误

java - 意外的值自动舍入

scala - 实现固定大小、不可变且专用的向量

Java 自动装箱和使用运算符比较对象

java - int 与 int[] 区别 - 泛型内部自动装箱?

java - Java 中的自动装箱和加宽

java - 如何从对象数组中删除元素?

java - 将 int 分配给对象时,Java 会自动装箱吗?