Java programmers should use
Java supports some primitive types that are not objects ... boolean, byte=
short, int, long, float, double.
Sometimes you will need to wrap up some primitive data in order to store
it into a data structure. Possibly the most frequent usage is to take a
primitive data value and store it in a hash table.
hash.put(=22lovesJmol=22, new Boolean(true));
This process of wrapping an object around a primitive is called 'boxing',=
because you put the primitive data value in a box so that it can be
managed and stored like all of the other object boxes. Java provides
classes for boxed primitive types, which are Capitalized like other
classes ... Boolean, Byte, Short, Integer, Long, Float, Double.
(One of the new language features associated with Java 5 (or 1.5) is that=
the compiler will automatically box and unbox primitive data types when
Each of these classes has some 'public final static' constants, such as
The Boolean class has two constants that are worth using:
These are boxed versions of true and false, respectively.
This means that you do not need to say:
The code looks a little cleaner if you refer directly to the constant. It=
may also be more efficient because you eliminate the runtime 'new'
operation. (Although, in reality there is a good chance that a modern Jav=
compiler would optimize that away).
From: Nicolas Vervelle <nvervell@cl...> - 2006-04-29 12:57:48
> Java programmers should use
> instead of
> new Boolean(true)
... snip ...
Agreed, and one more precision.
There's also Boolean.valueOf(boolean value) that should be used to
convert a boolean variable to a Boolean.