java布尔构造函数如何导致性能问题?
我正在尝试对我的代码运行IntelliJ中提供的代码检查,它报告了调用new Boolean("true")
时的性能问题
IDE中的描述如下所示
Reports any attempt to instantiate a new Boolean object. Constructing new Boolean objects is rarely necessary, and may cause performance problems if done often enough.
想了解此语句如何或为什么会导致性能问题吗
# 1 楼答案
如果调用
new Boolean("true")
一百万次,则创建了一百万个Boolean
对象。相反,您可以使用Boolean.valueOf("true")
,它将重用相同的Boolean
对象(或者只使用原语值true
,让编译器为您处理装箱)