Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Taming Tiger, Part 1

An introduction to Java 1.5

  • Print
  • Feedback

Page 2 of 6

// A list that stores numbers
// Generics will make this obvious...
List numbers = new ArrayList();
numbers.add(89);


Prior to version 1.5, this code would not compile since ArrayList's add method expects an object. To make it work, you must modify the second line as follows:

   numbers.add(new Integer(89));


That seems unnecessary and, with J2SE 1.5, it is. With J2SE 1.5, the compiler automatically adds the code to convert the integer value (such as 89) into the proper class (Integer in our example). That is called boxing. The opposite process of converting an object (such as of type Integer) into a value (such as an int) is called unboxing. Boxing and unboxing eliminate the (frequent and often burdensome) need to explicitly convert data of primitive type to reference and vice versa. Such explicit required conversions are verbose and induce clutter in the program text. The following table shows the rules for boxing and unboxing.

  • Print
  • Feedback

Resources