编程语言
首页 > 编程语言> > 在Java中应该避免自动装箱吗?

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

作者:互联网

在某些情况下,方法期望原始类型为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,autoboxing
来源: https://codeday.me/bug/20191010/1886815.html