Archive for the ‘J2SE 5’ Category

Highlights of J2SE 5.0

  • Metadata: Additional data or annotations can be associated with classes, interfaces, methods and fields. This can be read by annotation tools or at runtime through Java Reflection API. This can be used for generating additional source code or for providing additional debugging information.

@debug(devbuild=production,counter=1) public void testMethod()

  • Generic Types:  Earlier version’s Collection API stored the values as the instances of ‘OBJECT’ and so required type casting while fetching the objects. The mismatch of data types therefore could not be discovered until runtime. This is now replaced with a possibility to declare the Collection with <> notation that will detect the mismatch of data types at compile time.  

ArrayList<Integer> list =  new ArrayList<Integer>();
list.add(0, new Integer(42));
int total = list.get(0).intValue();

  • Auto-boxing and auto-unboxing of primitive type variables: The conversion between primitive type variables and their object based counterparts is left to the compiler now and will not require extra coding effort.

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0, 42);
int total = list.get(0);

  • Enhanced for Loop: The enhanced for loop will now replace the traditional iteration code used to traverse a collection. There is no need of looping code and type casting as the collection objects will be converted to generic types.

ArrayList<Integer> list = new ArrayList<Integer>();  
for (Integer i : list) { … }

  • Enumerated types: Enumerated type is introduced as an alternative to static final constants.

public enum StopLight { red, amber, green };

  • Static import: The static import feature enables to refer to static constants from a class without needing to inherit it.

import static java.awt.BorderLayout.*;
getContentPane().add(new JPanel(), CENTER);

  • Formatted Output:  The traditional C specific ‘printf’ functionality can be used for formatted output.

System.out.printf(“name count%n”);
System.out.printf(“%s %5d%n”, user,total);

  • Formatted Input:   In contrast to the heavy code used to get input from standard input in earlier versions, the scanner API provides basic input functionality for reading data from standard input or other data stream.

Scanner s= new Scanner(System.in);
String param= s.next();
int value=s.nextInt();

  • Varargs:  It allows to pass flexible number of arguments to a method.

void argtest(Object … args){}

  • Concurrency utilities:  The new version comes with more powerful concurrency control techniques like semaphores. It can be used to restrict access to a block of code, allows a defined number of concurrent thread accesses, and allows testing a lock before acquiring it.

final  private Semaphore s= new Semaphore(1, true);
s.acquireUninterruptibly(); //for non-blocking version use s.acquire()
try {    
      balance=balance+10; //protected value
} finally {
     s.release(); //return semaphore token

  • RMI compiler-rmic: There is no need to pre-generate stubs using rmic tool as the new version comes with the support of dynamic generation of stubs at runtime.
  • Class-data sharing:  This feature of class-data sharing enables the sharing of read-only data between multiple running JVMs and also improves startup time as core JVM classes are pre-packed.

  • Monitoring:   The JVM Monitoring and Management API enables monitoring and managing the Java virtual machine and the underlying operating system.  The API enables applications to monitor themselves and enables JMX-compliant tools to monitor and manage a virtual machine locally and remotely including low memory detector.
  • JVM Profiling API (JVMTI):  It includes byte code instrumentation. The advantage of this technique is that it allows more focused analysis and limits the interference of the profiling tools on the running JVM. The instrumentation can even be dynamically generated at runtime, as well as at class loading time, and pre-processed as class files.
  • Improved diagnostic capability:  New APIs are introduced to provide complete stack trace in case of any failure. The HotSpot JVM includes a fatal error handler that can run a user-supplied script or program if the JVM aborts.
  • Desktop Client:  New look and feel like introduction of a new theme for swing toolkit called ‘Ocean’.
  • Core XML support:  Several revisions to core XML support including XML 1.1, SAX 2.02, DOM level 3 support and XSLT with a fast XLSTC compiler.
  • Supplementary character support:  32-bit supplementary character support has been added. Supplementary characters are encoded as a special pair of UTF16 values to generate a different character A surrogate pair is a combination of a high UTF16 value and a following low UTF16 value. In general, when using a String or sequence of characters, the core API libraries will transparently handle the new supplementary characters for you.

String u=”\uD840\uDC08″;

  • JDBC rowsets:  There are five new rowset objects added in the new version.
    Two of the most valuable are:

    • Cached rowset: It contains an in-memory collection of rows retrieved from the database that can, if needed, be synchronized at a later point in time.
    • Web rowset: It can write and read the RowSet in XML format.

Read Full Post »