The autoboxing and unboxing feature, introduced in J2SE 5.0, facilitates the process of handling primitive data types in collections. We can use this feature to convert primitive data types to wrapper class types automatically. The compiler generates a code implicitly to convert primitive type to the corresponding wrapper class type and vice-versa. For example, consider the following statements:
Double d_object = 98.42;
double d_primitive = d_object.doubleValue( );
Using the autoboxing and unboxing feature, we can rewrite the above code as:
Double d_object = 98.42;
double d_primitive = d_object;
Data structures such as Stack, List, Queue used to accept only objects as parameters with their methods. But using autoboxing and unboxing one can directly "add" primitives to a data structure. For example, if an object s1 is created using the following statement,
Stack
Both, the following statements would mean same
s1.push(new Integer(10)); Or s1.push(10);
0 comments:
Post a Comment