Tuesday, January 13, 2009

Java 5 Features Esay to Development

Java 5 Features

java 5 has so many features these make development easy and faster.

1: Generics: In Java1.4 Collection work with Object class Type in this case you need to casting object from Collection Type object and this is unsafe. and The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.


Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.

How to Define Generics Type Collection in Java 5

List =new ArrayList();
Here ArrayList will contain only String Type object not any Other type.

2: Enhancement of Loop:
the enhanced for loop allows you to iterate through a collection without having to create an Iterator or without having to calculate beginning and end conditions for a counter variable. The enhanced for loop is the easiest of the new features to immediately incorporate in your code.

Suppose you have a collection of TechTip objects called RecentTips. You could use an enhanced for loop with the collection as follows:

Old forLoop:

public class OldForArray {
public static void main(String[] args){
int[] squares = {0,1,4,9,16,25};
for (int i=0; i< j =" 0;" squares =" {0,"> squares = new ArrayList();

private static void fillList() {
for (int i = 0; i < j="0;">To remove elements as you traverse collections
>To modify the current slot in an array or list
>To iterate over multiple collections or arrays

3: AutoBoxing: is the automatic conversion the Java compiler makes between the primitive (basic) types and their corresponding object wrapper classes (eg, int and Integer, double and Double, etc). The underlying code that is generated is the same, but autoboxing provides a sugar coating that avoids the tedious and hard-to-read casting typically required by Java Collections, which can not be used with primitive types.

Int x = new Integer(2); no need of conversion.

4: Typesafe Enum: Enumerations are sets of closely related items. Enum type accept only
predefine type constant. enum is typesafe.
for example

public enum Color {
WHITE, BLACK, RED, YELLOW, BLUE; //; is optional
}

Enum that overrides toString method.
A semicolon after the last element is required to be able to compile it. More details on overriding enum toString method can be found here
public enum Color {
WHITE, BLACK, RED, YELLOW, BLUE; //; is required here.

@Override public String toString() {
//only capitalize the first letter
String s = super.toString();
return s.substring(0, 1) + s.substring(1).toLowerCase();
}
}

Enum with additional fields and custom constructor.
Enum constructors must be either private or package default, and
protected or public access modifier is not allowed. When custom
constructor is declared, all elements declaration must match that
constructor.public enum Color {
WHITE(21), BLACK(22), RED(23), YELLOW(24), BLUE(25);

private int code;

private Color(int c) {
code = c;
}

public int getCode() {
return code;
}Enum that implements interfaces. Enum can implement any interfaces. All enum types implicitly implements java.io.Serializable, and java.lang.Comparable.public enum Color implements Runnable {
WHITE, BLACK, RED, YELLOW, BLUE;

public void run() {
System.out.println("name()=" + name() +
", toString()=" + toString());
}
}A sample test program to invoke this run() method:
for(Color c : Color.values()) {
c.run();
}Or,
for(Runnable r : Color.values()) {
r.run();
}

5:Varargs:

6: Static Import:

The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:

import static java.lang.Math.PI;
or en masse:
import static java.lang.Math.*;
Once the static members have been imported, they may be used without qualification:
double r = cos(PI * theta);

The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing
them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class
qualification.

6: Annotation: Java 5 introduces annotation types which can
be used to express metadata relating to program members in the
form of annotations. Annotations in Java 5
can be applied to package and type declarations (classes,
interfaces, enums, and annotations), constructors, methods,
fields, parameters, and variables. Annotations are specified in the
program source by using the @ symbol. For example,
the following piece of code uses the @Deprecated
annotation to indicate that the obsoleteMethod()
has been deprecated:
@Deprecated
public void obsoleteMethod() { ... }
Annotations may be marker annotations, single-valued annotations, or multi-valued annotations. Annotation types with no members or that provide default values for all members may be used simply as marker annotations, as in the deprecation example above. Single-value annotation types have a single member, and the annotation may be written in one of two equivalent forms:
@SuppressWarnings({"unchecked"})
public void someMethod() {...}
or
@SuppressWarnings(value={"unchecked"})
public void someMethod() {...}
Multi-value annotations must use the member-name=value syntax to specify annotation values. For example:
@Authenticated(role="supervisor",clearanceLevel=5)

public void someMethod() {...}












No comments: