Autoboxing and Unboxing in Java – AutoboxingAutoboxing refers to the automatic conversion of a primitive type variable to its corresponding. Since JDK version 5, Java provides two features, called autoboxing and auto- unboxing. They typically mean automatic conversion between. Java introduced a special feature of auto conversion of primitive types to the corresponding Wrapper class and vice versa. Autoboxing: Automatic.
|Published (Last):||28 December 2014|
|PDF File Size:||7.90 Mb|
|ePub File Size:||1.95 Mb|
|Price:||Free* [*Free Regsitration Required]|
Java uses primitive data types such as int, double, float etc. Despite the performance benefits offered by the primitive data types, there are situations when you will need an object representation of the primitive data type.
For example, many data structures in Java operate on objects.
So you cannot use primitive data types with those data structures. To handle such type of situations, Java provides type Wrappers which provide classes that encapsulate a primitive type within an object. Above mentioned Classes comes under Numeric type wrapper.
These classes encapsulate byte, short, int, long, float, double primitive type. Autoboxing and Unboxing features was added in Java5. Autoboxing is a process by which primitive type is automatically encapsulated boxed into its equivalent type wrapper Auto-Unboxing is a process by which the value of nava object is automatically extracted from a type Wrapper class.
Autoboxing and Unboxing in Java –
Whenever we use object of Wrapper class in an expression, automatic unboxing and boxing is done by JVM. When we perform increment operation on Integer object, it is first unboxed, then incremented and then again reboxed into Integer type object.
This will happen always, when we will use Wrapper class objects in expressions or conditions etc. It encapsulates primitive type char within object.
Character char ch Boolean: It encapsulates primitive type boolean within object. Boolean boolean boolValue Numeric type wrappers: It is the most commonly used type wrapper.
Autoboxing and Unboxing Autoboxing and Unboxing features was added in Java5.
Autoboxing and Unboxing in Java
We don’t have to perform Explicit typecasting. It helps prevent errors, but may lead to unexpected results sometimes.
Hence must be used with care. Auto-unboxing also allows you to mix different types of numeric objects in an expression. When the values are unboxed, the standard type conversions can be applied.