Tryst with Java Language

Java rite way

Boxing & UnBoxing — February 23, 2016

Boxing & UnBoxing

  • Boxing?
    output_u5b194
    Converts expressions of primitive type to corresponding expressions of reference type.
    int intValue = 0;
    Integer intObject = intValue;bytecode:For Boxing Java compiler uses valueOf() instead of constructor. This is beneficial since it allows for caching, and doesn’t force the creation of a new object on each boxing operation.

  • Un-Boxing?
    output_IIY77l
    Converts expressions of reference type to corresponding expressions of primitive type.
    Integer intObject = new Integer(0);
    int intValue = intObject;

    bytecode:
    For Un-Boxing Java compiler uses intValue(), which Returns the value of this Integer as an int.
Reference: