有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    如果调用new Boolean("true")一百万次,则创建了一百万个Boolean对象。相反,您可以使用Boolean.valueOf("true"),它将重用相同的Boolean对象(或者只使用原语值true,让编译器为您处理装箱)