Thursday 18 October 2012

Java 5 - What's New

J2SE Version 5.0

Code named Tiger and released on September 30, 2004.


  • Generic— enhance compile time type checking and eliminates the need for casting every time we get an object out of Collections.
  • Autoboxing/unboxing— eliminates need of manual conversion between primitive types (such as int) and wrapper types (such as Integer), improves readability.
  • Enhanced For loop— eliminates error-proneness of iterators, Improves readability, reducing of writing unnecessary codes and it works with both arrays as well as objects that expose an iterator.
  • Static import— improves utility functions, relieve need for implementing a Constant Interface
  • Varargsallows to improve API usability and Variable args allow formatted I/O.
  • Typesafe enumsenumeration is a list of named constants, it improves readability and organization of constants.
  • Metadata—allows programmers to avoid writing boiler unnecessary codes and gives the opportunity to developers for declarative programming.
Some of other minor tweaks and incremental upgrades of JDK 5 are as follows:-


  • Instrumentation
  • StringBuilder class in jdk 1.5 (java.lang package)
  • Swing: new skinnable look and feel, called synth and Ocean, a new theme for Metal- beyond look and feels.
  • Automatic stub generation for rmi objects.
  • Scanner class for parsing data from various input streams and buffers.
  • Three new interfaces have been added to the Collection framework which is Queue, BlockingQueue, and ConcurrentMap. Queue is intorduce in java.util package where as BlockingQueue, and ConcurrentMap is available in java.util.concurrent package.
  • The Queue implementation classes are AbstractQueue, PriorityQueue, ConcurrentLinkedQueue where as  BlockingQueue implementation classes are ArrayBlockingQueueDelayQueue, LinkedBlockingQueuePriorityBlockingQueueSynchronousQueue.
  • ConcurrentHashMap is the implementation of ConcurrentMap.
  • Special purpose List and Set implementations are added like CopyOnWriteArrayListand CopyOnWriteArraySet. For example refer : - http://javatechtipssharedbygaurav.blogspot.in/2012/10/copyonwritearraylist-and.html

Demo exampleof Generics, Autoboxing/unboxing, Enhanced For loop and Static import

package com.gaurav.jdk5features;

import static java.lang.System.out;
import java.util.ArrayList;
import java.util.List;

public class ImplementaionOfJava5Features {
    public static void main(String[] args) {

        Integer testAutoboxing = 500; // demo of autoboxing, I am assigning int value
        out.println("integer Value is - " + testAutoboxing); // Use of Static Import
                                                               

        int testUnBox = testAutoboxing; // demo of auto-unboxing
        out.println("int value is - " + testUnBox); // Use of Static Import

        List<String> lst = new ArrayList<String>(0); // Use of Generics
        lst.add("Kumar");
        lst.add("Gaurav");
        lst.add("Shivam");
        for (String str : lst) // Use of Enhanced For loop
            out.println("Added elements are-" + str); // Use of Static Import
    }
}

Demo example of  Varargs(Variable Arguments):-

package com.gaurav.jdk5features;

import static java.lang.System.out;

public class ImplementaionOfJava5FeatureVarargs {
    static void variableArgumetTestForInt(int... num) { //Use of Varargs
        out.println("the variable length ->" + num.length);
        for (int number : num) {
            out.println("int arguments are-" + number);
        }
    }

    static void variableArgumetTestForBoolean(boolean... flag) { //Use of Varargs
        out.println("boolean variable length ->" + flag.length);
        for (boolean f : flag) {
            out.println("boolean arguments are-" + f);
        }
    }

    public static void main(String args[]) {
        variableArgumetTestForInt(1, 2);    //Use of Varargs
        variableArgumetTestForInt(11, 28, 56);    //Use of Varargs
        variableArgumetTestForBoolean(true, false);    //Use of Varargs

    }

}

Demo example of  Typesafe enums

package com.corejava.gaurav.examples;

public class ImplementaionOfJava5FeatureEnumType {
    public static void main(String args[]) {
        System.out.println("Gaurav Age is - " + Name.Gaurav.getAge());

    }

    enum Name { // Use of enum type
        Gaurav(33), Kumar(19), Raju(45);
        private int age;

        Name(int ageVal) {
            age = ageVal;
        }

        int getAge() {
            return age;
        }

    }

}

Demo example of  Metadata(Annotations)

package com.gaurav.jdk5features;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
// Use of Metadata(Annotations)
@interface MyFirstAnnotation {
    String strTestValue();

    double doubleTestval();
}

public class ImplementaionOfJava5FeatureAnnotation {
    @MyFirstAnnotation(strTestValue = "Two Parameters", doubleTestval = 3.275)
    public static void annotationUseMethod() {
        ImplementaionOfJava5FeatureAnnotation annotationsTest = new ImplementaionOfJava5FeatureAnnotation();
        // obtaining the annotation for the method annotationUseMethod()
        // displaying the values of the annotated members.
        try {
            Method m = annotationsTest.getClass().getMethod(
                    "annotationUseMethod");
            MyFirstAnnotation anno = m.getAnnotation(MyFirstAnnotation.class);

            System.out.println("String value is - '" + anno.strTestValue()
                    + "' and double value is - " + anno.doubleTestval());
        } catch (NoSuchMethodException nex) {
            System.out.println(nex.getMessage());
        }
    }

    public static void main(String args[]) {
        annotationUseMethod();
    }

}


No comments:

Post a Comment