Skip to main content

Java Autoboxing and Annotation

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 require 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‘ 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 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 streamline 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 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 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 take 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 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 extendsclause. However, it is automatic that all annotation programs extend the Annotation interface. Thus Annotation is the superclass of all annotations.

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...