G.S
G.S

Reputation: 10871

How Boolean constructors can cause performance issue?

I am trying to run the code inspection provided in the IntelliJ on my code and it reported performance issue on calling new Boolean("true").
The description in IDE is given as

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.

Want to understand how or why this statement may cause the performance issue?

Upvotes: 1

Views: 210

Answers (1)

Eran
Eran

Reputation: 393811

If you call new Boolean("true") a million times, you are creating million Boolean objects. Instead you can use Boolean.valueOf("true") which will reuse the same Boolean object (or just use the primitive value true and let the compiler handle the boxing for you).

Upvotes: 3

Related Questions