Features of Java 5

• Generics
• For each Loop
• Auto boxing and Unboxing
• Enums
• Var-args
• Static Import
• Annotations

Generics

Generics programming enables classes and methods to operate on well defined parameters , hence allowing user to add only a specific type of data at compilation time.

Hence we can say that ,it  prevent unnecessary type- casting.

Without Generics :
List ls=new ArrayList();
ls.add("Hello");
Integer x=(Integer)ls.get(0);

This will generate run time error as in third line, String is being supplied to be converted into Integer.

Now using Generics :
List ls=new ArrayList();
ls.add("Hello");
Integer x=ls.get(0);

This will generate Compilation error because in third line as get(0) returns String,that is to be stored in x. Hence saved from run time error.

The reason behind error is that we have already specified that ArrayList can take only String type values.If we try to put value of someother type other than specified type it would not accept it and generate error.

Click to Learn Generics in Detail.


For each loop

For loop is Mainly used to traverse array or collection elements.

The advantage of for-each loop is that it makes the code more readable .

Normal for loop :
for(int i=1;i<=5;i++){}

Enhanced for loop :
Syntax : for(declaration:expression)
{
statement;
}

Example :
int n={1,2,3,4};
for(int a:n)
{
System.out.println(a);
}

Note : We can also use break within for loop to exit the loop,depending upon certain condition.

Click to learn For Each loop in detail.


Autoboxing and Unboxing


The automatic conversion of primitive data types into its equivalent Wrapper type is called boxing.

Example:
int a=2;
Integer b=a;
or
Integer c=4;

Note : Wrapper class is used when a particular data type need to be treated as object.

The conversion of the reference type to the corresponding primitive data type is called Unboxing.

Click to learn Autoboxing and Unboxing in detail.


Enums


Enum is the keyword used to represent fixed number of values.

Advantages of enums :

1. Type safety : Enums provide compile-time type safety and prevent from comparing constants in different enums.
2. Limits inputs :
The compiler would not allow parsing a constant of a wrong enum to a method that expects an enum type.
    When enum is used in switch statement, the enum constants are the only allowed labels for cases.
    IDEs like eclipse even provide autocomplete feature for filling them.
3. Enums groups things in a set.
4. Enums are iterable.

Click to learn Enum in Detail


Var-args


Var-args also called variable arguments, are used to write those methods that can accept as many arguments as needed, during runtime.
Syntax : void ab(String ... a){ }
The 3 dots"..." called ellipsis implies that method can accept any number of arguments.
The major advantage of var-args is that there is no need of method overloading .

Click to learn Var-args in detail.



Static Import


The static import facilitate the java programmer to access any static member of a class directly.
There is no need to qualify it by the class name.

Difference between import and Static import ?
The import allows the java programmer to access classes of a package without package qualification whereas the static import feature allows to access the static members of a class without the class qualification.
The import provides accessibility to classes and interface whereas static import provides accessibility to static members of the class.

import static java.lang.System.*;
public class StaticImportExample
{
      public static void main(String args[])
      {
       out.println("Static Import Example"); //No need to use System. Qualifier
       out.println("Java");
        }
}

Static import makes it easier to code, but sometimes too much of its usage can make the code very difficult to read.

Code without Static import:


Output



We need to use class_name.method_name.


Code with static import:


Output


Annotation


Annotation is metadata about the program itself that means the organized data about the code, embedded within the code. It can be parsed by the compiler.

We have basic java comments infrastructure using which we add information about the code . But Annotations are more powerful than java comments.

Annotation Types:

1.Documented
When a annotation is annotated with @Documented then wherever this is used, elements needs to be documented using Javadoc tool.

2.Inherited
This meta annotation denotes that the annotation type can be inherited from super class. When a class is annotated with Inherited, then its super class will be queried till a matching annotation is found.

3.Retention
This meta annotation denotes the level till which this annotation will be carried.

• Built-in Java Annotations

@Documented, @Inherited, @Retention and @Target are the four available meta annotations that are built-in with Java.
Apart from these meta annotations we have the following annotations.

@Override
When we want to override a method, we can use this annotation to say to the compiler that we are overriding an existing method.
If the compiler finds that there is no matching method found in super class then it generates a warning. This is not mandatory to use @Override when we override a method. but it is considered as a best practice.

@Deprecated

When we want to inform the compiler that a method is deprecated we can use this. So, when a method is annotated with @Deprecated and that method is found used in some place, then the compiler generates a warning.

@SuppressWarnings
This is used when We want the compiler not to raise any warnings.