Manual conversion between primitive types (such as an int) and wrapper classes (such as Integer) is necessary when adding a primitive data type to a collection. As an example, consider an int being stored and then retrieved from an ArrayList:
list.add(0, new Integer(59));
int n = ((Integer)(list.get(0))).intValue();
The new autoboxing/unboxing feature eliminates this manual conversion. The above segment of code can be written as:
list.add(0, 59);
int total = list.get(0);
However, note that the wrapper class, Integer for example, must be used as a generic type:
List
Programming language critics may have a lot to say about the autoboxing/unboxing feature. On one hand, Java developers agree that the distinction between primitive data types and references can be burdensome. In a pure object-oriented language there should be no difference between a primitive data type and a reference, as everything is an object. The other issue is that of identity: many think of primitive data types as entities that represent mathematical values, which are different from references.
The purpose of the autoboxing/unboxing feature, however, is simply to ease the interoperability between primitive types and references without any radical changes.
(http://java.sun.com/developer/technicalArticles/releases/j2se15langfeat/)
Wednesday, October 17, 2007
Autoboxing/Unboxing
Posted by sathish at 10/17/2007 07:43:00 AM
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment