In Java libraries there are many classes which instances (the objects, created from them) are IMMUTABLE. Means, once created, nothing inside the object can be changed. Such are java.lang.String, java.lang.Integer, java.lang.Boolean etc. Every object of class java.lang.Boolean can have only two meanings: Boolean.TRUE or Boolean.FALSE. Once created, the value (internally stored in a boolean field named "value") can never ever be changed. And when the VM loads (generally speaking) there are two static Boolean objects, each of which represents one of the possible boolean values (namely, Boolean.TRUE = true and Boolean.FALSE = false), so there is usually no need to create another instance of class Boolean. Since it will be either logically equal to Boolean.FALSE or to equal to Boolean.TRUE. That is why "new Boolean(...)" is rarely needed ...
Hello Kevin,
This avoids creating a new Boolean object and instead reuses one of the singleton
TRUE or FALSE instances.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
That makes sense. Thanks!
Kevin Hale Boyes wrote:
In Java libraries there are many classes which instances (the
objects, created from them) are IMMUTABLE. Means, once created, nothing
inside the object can be changed. Such are java.lang.String,
java.lang.Integer, java.lang.Boolean etc.
Every object of class java.lang.Boolean can have only two meanings:
Boolean.TRUE or Boolean.FALSE. Once created, the value (internally
stored in a boolean field named "value") can never ever be changed. And
when the VM loads (generally speaking) there are two static Boolean
objects, each of which represents one of the possible boolean values
(namely, Boolean.TRUE = true and Boolean.FALSE = false), so there is
usually no need to create another instance of class Boolean. Since it
will be either logically equal to Boolean.FALSE or to equal to
Boolean.TRUE. That is why "new Boolean(...)" is rarely needed ...
Hope this was helpful ;)
Greetings,
George