Autoboxing and Unboxing:The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing. This is the new feature of Java5. So java programmer doesn't need to write the conversion code. Advantage of Autoboxing and Unboxing:No need of conversion between primitives and Wrappers manually so less coding is required. |
Simple Example of Autoboxing in java:Test it Now
Simple Example of Unboxing in java:The automatic conversion of wrapper class type into corresponding primitive type, is known as Unboxing. Let's see the example of unboxing: Test it NowOutput:
Autoboxing and Unboxing with comparison operatorsAutoboxing can be performed with comparison operators. Let's see the example of boxing with comparison operator: | Test it Now
Autoboxing and Unboxing with method overloadingIn method overloading, boxing and unboxing can be performed. There are some rules for method overloading with boxing:- Widening beats boxing
- Widening beats varargs
- Boxing beats varargs
|
1) Example of Autoboxing where widening beats boxingIf there is possibility of widening and boxing, widening beats boxing. | Test it Now
2) Example of Autoboxing where widening beats varargsIf there is possibility of widening and varargs, widening beats var-args. | Test it Now
3) Example of Autoboxing where boxing beats varargsLet's see the program where boxing beats variable argument: | Test it Now
Method overloading with Widening and BoxingWidening and Boxing can't be performed as given below: | Test it NowOutput:Compile Time Error
|