Sunday, March 13, 2011

JAVA 7 Features


Virtul Machine

JSR 292: Support for dynamically-typed languages (InvokeDynamic)

VM and language extensions to support the implementation of dynamically-typed languages at performance levels near to that of the Java language itself

Strict class-file checking [NEW]
Per the Java SE 6 specification, class files of version 51 (SE 7) or later must be verified with the typechecking verifier introduced by JSR 202 in Java SE 6; the VM must not fail over to the old inferencing verifier

Language

JSR 334: Small language enhancements (Project Coin)

A set of small language changes intended to simplify common, day-to-day programming tasks:

1.      Strings in switch statements
2.      Automatic resource management
3.      Improved type inference for generic instance creation ("diamond")
4.      Simplified varargs method invocation,
5.      Better integral literals,
6.       Improved exception handling (multi-catch)

Core

Upgrade class-loader architecture

Modifications to the ClassLoader API and implementation to avoid deadlocks in non-hierarchical class-loader topologies

Method to close a URLClassLoader

A method that frees the underlying resources, such as open files, held by a URLClassLoader

Concurrency and collections updates (jsr166y)

A lightweight fork/join framework, flexible and reusable synchronization barriers, transfer queues, a concurrent-reference HashMap, and thread-local pseudo-random number generators

Internationalization

Unicode 6.0

Upgrade the supported version of Unicode to 6.0

Locale enhancement

Upgrade the java.util.Locale class to support IETF BCP 47 and UTR 35 (CLDR/LDML)

Separate user locale and user-interface locale

Upgrade the handling of locales to separate formatting locales from user-interface language locales, as is done on Vista and later versions of Windows

I/O and Networking

JSR 203: More new I/O APIs for the Java platform (NIO.2)

New APIs for filesystem access, scalable asynchronous I/O operations, socket-channel binding and configuration, and multicast datagrams

NIO.2 filesystem provider for zip/jar archives

A fully-functional and supported NIO.2 filesystem provider for zip and jar files

SCTP (Stream Control Transmission Protocol)

An implementation-specific API for the Stream Control Transmission Protocol on Solaris

SDP (Sockets Direct Protocol)

Implementation-specific support for reliable, high-performance network streams over Infiniband connections on Solaris and Linux

Use the Windows Vista IPv6 stack

Upgrade the networking code to use the Windows Vista IPv6 stack, when available, in preference to the legacy Windows stack

TLS 1.2

Add support for TLS 1.2, which was standardized in 2008 as RFC 5246

Security & Cryptography

Elliptic-curve cryptography (ECC)

A portable implementation of the standard Elliptic Curve Cryptographic (ECC) algorithms, so that all Java applications can use ECC out-of-the-box

JDBC

JDBC 4.1

Upgrade to JDBC 4.1 and Rowset 1.1

Client

XRender pipeline for Java 2D

A new Java2D graphics pipeline based upon the X11 XRender extension, which provides access to much of the functionality of modern GPUs

Create new platform APIs for 6u10 graphics features

Create new platform APIs for features originally implemented in the 6u10 release: Translucent and shaped windows, heavyweight/lightweight mixing, and the improved AWT security warning

Nimbus look-and-feel for Swing

A next-generation cross-platform look-and-feel for Swing

Swing JLayer component

Add the SwingLabs JXLayer component decorator to the platform

Web

Update the XML stack

Upgrade the JAXP, JAXB, and JAX-WS APIs to the most recent stable versions

Management

Enhanced JMX Agent and MBeans [NEW]

An implementation-specific enhanced JMX management agent, ported from JRockit, which makes it easier to connect to the platform MBean server through firewalls, together with a richer set of MBeans which expose additional information about the internal operation of the VM

Deferred to JDK 8 or later

JSR 294: Language and VM support for modular programming

Enhancements to the Java language and virtual-machine specifications to support modular programming, at both compile time and run time

JSR 308: Annotations on Java types

An extension to the Java annotation syntax to permit annotations on any occurrence of a type

JSR TBD: Language support for collections

Literal expressions for immutable lists, sets, and maps, and indexing-access syntax for lists and maps

JSR TBD: Project Lambda

Lambda expressions (informally, "closures") and defender methods for the Java programming language

Modularization (Project Jigsaw)

A simple, low-level module system focused upon the goal of modularizing the JDK, and the application of that system to the JDK itself

JSR 296: Swing application framework

An API to define the basic structure of a typical Swing application, thereby eliminating lots of boilerplate code and providing a much-improved initial developer experience

Swing JDatePicker component

Add the SwingLabs JXDatePicker component to the platform



Thursday, June 3, 2010

Singleton - Multiple Users [Thread Safe]

Following is the way by which we can use Singleton Design pattern for multiple users.

public class Singleton {
         private volatile static Singleton singleton; //volatile is needed so that multiple thread can reconcile the instance
         private Singleton(){
         }

         public static Singleton getSingleton(){ //synchronized keyword has been removed from here
         if(singleton = = null)
         {          //needed because once there is singleton available no need to aquire monitor again & again as it is costly
                synchronized(Singleton.class)
                {
                        //this is needed if two threads are waiting at the monitor at the time when singleton was getting instantiated
                      if(singleton==null)
                      {      
                              singleton= new Singleton();
                       }
                  }
          }
          return singleton;
    }
}

Thursday, May 13, 2010

wait(), notify() and notifyAll()

The wait(), notify() and notifyAll() methods are used to provide an efficient way for threads to wait for a shared resource. When a thread executes an object's wait() method, it enters the waiting state. It only enters the ready state after another thread invokes the object's notify() or notifyAll() methods.

Monday, May 10, 2010

Volatile Modifiers in Java

Volatile Modifier

       The volatile modifier requests the Java Virtual Machine to always access the shared copy of the variable so the its most current value is always read. If two or more threads access a member variable, and one or more threads might update that variable’s value, and all of the threads do not use synchronization to read and/or write the variable value, then that member variable must be declared volatile to ensure all threads should get the updated value.
       We will discuss the java volatile modifier using following example.
       In our example, we take two threads Thread T1 and Thread T2 accessing member variable x of class C and here Thread T1 is not using synchronization and Thread T2 uses synchronization. If Thread T2 updates value of variable x from 0 to 1. But if the variable x is not declared as volatile then meanwhile Thread T1 tried to access variable then it will get value of variable x as 0. But as variable x is updated to 1 by Thread T2, To avoid this mess the variable x should be declared as volatile so Thread T1 should get updated value 1.

Thursday, May 6, 2010

HashMap vs ConcurrentHashMap

Both HashMap and ConcurrentHashMap are inherits some characteristics of Hashtable. But they have some prominent differences in context with performance, scalability.

HashMap
  1. Since Java 1.2
  2. Allows null key and value
  3. Poor performance in highly threaded applications
  4. Not much scalable
  5. Throws a ConcurrentModificationException
  6. Faster in non-multi threading applications

ConcurrentHashMap
  1. Since Java 1.5
  2. Doesn't allow null key or value
  3. Better performance in Highly threaded applications
  4. Highly scalable
  5. Do not throw ConcurrentModificationException
  6. Slower in non-multi threading applications


Wednesday, May 5, 2010

Read value from Java properties

Here, you will find the source code to read value of the key from properties file. java.util.Properties class extends Hashtable. Properties class creates the properties file which stores key - value pair using Hashtable. To retrieve value from properties file we have to follow following steps.
  1. Create an object of Properties class.
  2. Load properties file using FileInputStream into properties object.
  3. Get value from properties object using getProperty() method passing key as an input.
The below code will help to understand more...

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class PropertiesReadTest {

    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("C:\\temp\\test.properties"));
            String value = properties.getProperty("key");
            System.out.println("Value = " + value);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output is shown here in following image





Tuesday, May 4, 2010

Source code to read content of File

Here you will find the source code to read the content of file.

Steps
  1. Create the FileReader object using file name as an input to FileReader constructor.
  2. Pass that FileReader object to BufferedReader.
  3. Read the line from BufferedReader object till readline gets null.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReadTest {

    public static void main(String[] args) {
         try {
            BufferedReader br = new BufferedReader(new FileReader("C:\\temp\\test.txt"));
            String line = "";
            while((line = br.readLine()) != null)
            {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}