Wednesday, 8 November 2017

Java Autoboxing and Annotation

November 08, 2017 Posted by Prakash No comments

Java Autoboxing and Annotation

With the beginning of JDK 5, Java added two more important features; Autoboxing and Auto unboxing.
These features are important and play a very important 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 opposite 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 required to convert between primitives and Wrappers manually and hence less coding is required.

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‘ 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 type is needed. There is no need to explicitly construct an object. 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 greatly streamlines the code of several algorithms, removing the tedium of manually boxing and unboxing values.

Autoboxing / Unboxing in Expressions

Whenever we use object of Wrapper class in an expression, automatic unboxing and boxing is 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 is 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 easy 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 takes place whenever a conversion into an object or form an object is required. This 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 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 acts much like fields. It is to be noted that annotation cannot include extends clause. However, it is automatic that all annotation programs extend the Annotationinterface. Thus Annotation is the super class of all annotations.

0 comments:

Post a Comment