Saturday, May 24, 2014

Boxing and Unboxing In Java

Before attempting to understand the concept of Boxing and UnBoxing in java, one needs to have an idea of the basic classification of datatypes in the language. The data types in java are broadly classified into two major types: 

1) Primitive types
2) Reference types/ object types

Primitive types include byte, short, int, long, float, double, char, boolean and String (String is not completely a primitive type but also reference type). Clear explanation of primitive types is given at this link.
Rest all the other types in java are classified as Reference types or objects types.

Some of the differences between primitive and reference types include:

  • It is not possible to create a primitive datatype of our own but it is possible in case of reference types or objects.
  • The variables of primitive types have a memory size that is fixed by the language specification etc. For example, a variable of type int is allocated a memory of 4 bytes. In case of reference types the size of object varies based on its class specifications.
  • The reference types are either core library defined or user defined. The value assigned to the variable of reference type will either be a reference to an object or a NULL value. The default values of primitive type variables can be zero but not NULL. A NULL value means that the variable is not referring to any current instance in the memory.

Boxing It is the process of placing a primitive datatype within an object so that it can be treated as an object.

What is the need of primitive datatype to be treated as an object ?
In Java, certain types are allocated on stack and others on heap (usually primitives types on stack and reference types on heap). The heap allocation is done during the run time. Sometimes there is a need to bring the stack allocated instances on to heap. In such cases, primitive types need to be converted into reference types. One of the examples for such kind of scenarios would be method invocations. Method invocations are done during run time and they require objects to perform this action, but if a primitive type is present then it must be converted into an object type. This conversion is known as Boxing.

Similarly in the statements involving increment or decrement operations, objects need to be converted into primitive types. This is because increment and decrement operations cannot be directly performed on objects. This kind of conversion is known as UnBoxing.

Many data structures in Java operate on objects (Example: Collections), so we cannot use primitive types with those data structures. To handle these situations java provides type wrappers which provide classes that encapsulate primitive types with in an object.

In the above figure, int is a primitive data type which is being wrapped in a Integer class so as to use it as a reference type. 

Simple examples of boxing and Unboxing

  1) Integer object_type = 3 ;    // boxing
  2) int primitive_value       =  object_type;    //unboxing
  3) List<Integer> integer_list = new ArrayList<>();

                    for(int i = 1; i < integer_list .size(); i++){
                    integer_list .add(Integer.valueOf(i));

In the above example the Collection ArrayList<> accepts only objects hence boxing is done by using the Integer wrapper class.
Fork me on GitHub