Summary
=======

Java programmers should use

  Boolean.TRUE

instead of

  new Boolean(true)


Detail
======
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("lovesJmol", 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
necessary.)

Each of these classes has some 'public final static' constants, such as
Integer.MAX_VALUE.

The Boolean class has two constants that are worth using:

  Boolean.TRUE
  Boolean.FALSE

These are boxed versions of true and false, respectively.

This means that you do not need to say:
  new Boolean(true)
OR
  new Boolean(false)

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 Java
compiler would optimize that away).


Miguel




-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Jmol-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jmol-developers

Reply via email to