Java Autoboxing and Annotation

With the beginning of JDK 5, Java added two more important features; Autoboxing and Auto unboxing.

These features are essential and play a significant role in dealing with primitive data types. In this chapter, you will learn about how to use these features within a Java source code.

What is boxing in Java?

The automatic adaptation of primitive data types into its corresponding Wrapper type is known as boxing, and reverse operation is known as unboxing. Since it is a new feature in Java; so Java programmers do not need to write the conversion code. One advantage of using this feature is that programmers do not require to convert between primitives and Wrappers manually and hence less coding is needed.

What are Wrappers in Java?

Wrapper class in Java provides the mechanism to convert primitive into object and object into primitive. A wrapper type/class of Java is one of 8 classes provided in the 'java.lang' The package used for creating objects for the eight primitive types. Wrapper classes are used to represent primitive values when an Object is required. The lists of all wrapper classes are:

  • short
  • byte
  • integer
  • long
  • float
  • double
  • boolean
  • character

What are Autoboxing and Auto unboxing?

Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrappers whenever an object of the type is needed. There is no need to construct an object explicitly. Autoboxing is the process by which the value of a boxed object is automatically extracted (unboxed) from a type wrapper when the program requires its value. Furthermore, autoboxing and auto-unboxing significantly streamline the code of several algorithms, removing the tedium of manually boxing and unboxing values.

Autoboxing / Unboxing in Expressions

Whenever we use the object of Wrapper class in an expression, automatic unboxing and boxing are done by JVM.

Here is a simple code snippet showing the autoboxing feature of Java:

Integer iOb;
iOb = 100;  //Autoboxing of int
++iOb;

When programmers perform incrementing of variables/objects of type integers, automatic boxing and unboxing are done by JVM, where the object is first unboxed then incremented and then again reboxed into integer type object.

Advantages of autoboxing and auto-unboxing

There are some benefits that this new feature of Java provides us. They are:

  • Less code to write
  • The code looks cleaner and easily readable
  • Programmers do not have to perform Explicit typecasting
  • The best method for conversion is automatically chosen
  • It helps prevent errors but may lead to unexpected results sometimes.

One disadvantage of autoboxing and auto-unboxing is that when programmers mix wrappers with primitives, there becomes a mismatch between 'equal' and '==', which becomes difficult to recognize errors.

Autoboxing / Auto unboxing Occurs in Expressions

In general, autoboxing and auto-unboxing take place whenever a conversion into an object or form an object is required. It applies to expressions. Within an expression, a numeric object is automatically unboxed. The outcome of the object is automatically reboxed, when necessary.

Here is a simple program showing the working of autoboxing and auto-unboxing:

Example:
public class Main {
public static void main(String args[]) {
   Integer iOb = 60, iOb2; // autobox an int
   int i = iOb; // auto-unbox
   System.out.println(i + " " + iOb);
   iOb2 = iOb+(iOb/3);
  System.out.println(i + " " + iOb2);
 }
}

What is an annotation in Java?

With JDK 5, another new facility was provided that enables programmers to embed supplemental information into a source file. This information is called an annotation which does not change the action of a program. Thus annotation leaves the semantics of any program unchanged.

Here is the declaration of annotation named FirstAnno:

Example:
// A simple annotation type
 @interface FirstAnno{
  String str();
 int val();
}

The @ symbol preceding the keyword interface tells the compiler that an annotation type is being declared. There are also two members str() and val(). All annotation solely consists of method declaration and these methods act much like fields. It is to be noted that annotation cannot include extends clause. However, it is automatic that all annotation programs extend the Annotation interface. Thus Annotation is the superclass of all annotations.