Showing posts with label Java and It's Version - Features. Show all posts
Showing posts with label Java and It's Version - Features. Show all posts

Thursday, 18 October 2012

Java 6 - What's New



J2SE Version 6.0

Code named Mustang and released on December 11, 2006.

  • Scripting Language Support : Generic API for tight integration with scripting languages, and built-in Mozilla JavaScript Rhino integration . Scripting allows Java applications to invoke script engines dynamically through a "service discovery" mechanism. This allows developers to add scripts from Groovy, Python and Ruby in their applications. Now developers are having power to instantiate classes directly from a script. So the repetitive tasks can be automated and making the developers life easier.
  •   JDBC 4.0 support: - All JDBC drivers loading and registration is now handled by a new DriverManager class. Now no need to use "Class.forName()" function to manually register a driver. Now For SQL query strings, annotations can be used. Support for New data types (including XML and SQL ROWID).
  •   Java Compiler API: Now Using API, programmatically developers can select and invoke a Java Compiler by a Java program.Java SE 6 allows the compiler to receive input and/or send output to an abstraction of the file system
  •    JVM improvements include: synchronization and compiler performance optimizations, new algorithms and upgrades to existing garbage collection algorithms, and application start-up performance. 
  •   Upgrade of JAXB(Java Architecture for XML Binding) to version 2.0 : Including integration of a  StAX(Streaming API for XML) parser. 
  • XML digital signature API
  •   JDK6 includes support for pluggable annotations. Included Support Web services metadata for the Java Platform and Common Annotations for the Java Platform.
  •   Integrated support for Web Services through JAX-WS
  •  Performance improvements tweaks are added for the core platform and Swing. 
  •  GUI improvements, Like as integration of SwingWorker in the API, table sorting and filtering, and true Swing double-buffering. Included a feature of writing of GIF images and Improved drag-and-drop support.

Enhancements to the collections framework in Java SE 6 is as follows:-

The following new interfaces are included in the Collection framework.
  • Deque - a double ended queue, supporting element insertion and removal at both ends. Extends the Queue interface.
  • BlockingDeque - a Deque with operations that wait for the deque to become non-empty when retrieving an element, and wait for space to become available in the deque when storing an element. Extends both the Deque and BlockingQueue interfaces. (This interface is part of java.util.concurrent.)
  • NavigableSet - a SortedSet extended with navigation methods reporting closest matches for given search targets. A NavigableSet may be accessed and traversed in either ascending or descending order. This interface is intended to supersede the SortedSet interface.
  • NavigableMap - a SortedMap extended with navigation methods returning the closest matches for given search targets. A NavigableMap may be accessed and traversed in either ascending or descending key order. This interface is intended to supersede the SortedMap interface.
  • ConcurrentNavigableMap - a ConcurrentMap that is also a NavigableMap. (This interface is part of java.util.concurrent.)

The following new concrete implementation classes have been included in Collection framework.
  • ArrayDeque - efficient resizable-array implementation of the Deque interface.
  • ConcurrentSkipListSet - concurrent scalable skip list implementation of the NavigableSet interface.
  • ConcurrentSkipListMap - concurrent scalable skip list implementation of the ConcurrentNavigableMap interface.
  • LinkedBlockingDeque - concurrent scalable optionally bounded FIFO blocking deque backed by linked nodes.
  • AbstractMap.SimpleEntry - simple mutable implementation of Map.Entry
  • AbstractMap.SimpleImmutableEntry - simple immutable implementation of Map.Entry

Two new methods were added to the Collections utility class:
  • newSetFromMap(Map) - creates a general purpose Set implementation from a general purpose Map implementation.
  • asLifoQueue(Deque) - returns a view of a Deque as a Last-in-first-out (Lifo) Queue.
The Arrays utility class now has methods copyOf and copyOfRange that can efficiently resize, truncate, or copy subarrays for arrays of all types.

  Ref taken from :- wikipedia and oracle


Note:- Java 6 improves the programming environment, especially for JDBC and AWT/Swing programs

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 CopyOnWriteArrayList and 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();
    }

}