在某些情况下,方法需要基本类型 double
并且您将 Double
对象作为参数传递。
由于编译器对传递的对象进行拆箱,这会增加内存使用量还是降低性能?
最佳答案
这是 Java Notes 在 autoboxing 上所说的内容:
Prefer primitive types
Use the primitive types where there is no need for objects for two reasons.
- 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 - Java 中应该避免自动装箱吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7610664/