Box with J2SE 5.0
So, what is Boxing in Java? Boxing refers to the automatic conversion from a primitive to its corresponding wrapper type: Boolean, Byte, Short, Character, Integer, Long, Float or Double. Since this happens automatically, it's also referred to as autoboxing. The reverse is also true, ie. J2SE 5.0 automatically converts wrapper types to primitives and is known as unboxing.
Before you can try out this simple example, you must install and set the environment variables 'path' and 'classpath' for J2SE 5.0.
The example is quite trivial and you could try compiling it with an earlier version of J2SE, to see the compilation errors flagged out.
public class BoxingUnBoxingBoxing/Unboxing saves us the bother of converting from a primitive to it's wrapper and vice-versa. This feature is definitely a time-saver.
{
public static void main(String[] args)
{
// Boxing
int var = 10;
Integer wInt = var;
int result = var * wInt;
System.out.println("Value of result = " + result);
// Unboxing
int conv = wInt;
if (conv < 100)
System.out.println("True");
}
}
Update: Java 2 Platform, Standard Edition (J2SE 6.0), code name Mustang, is due in the first half of 2006. Also Dolphin, the Java SE 7 release is scheduled to follow Mustang in late 2007,
Technorati Tags: Box with J2SE 5.0
Blogs linking to this article