Monday, November 24, 2008

Understanding Java Classloading and Hierarchy

This article provides the basics of Java Classloading and it makes every successful Java programmer a foundation to understand Classloading behaviour.

Classloaders are fundamental part of the Java Virtual Machine (JVM) that loads classes into memory and is responsible for finding and loading class files at run-time.

Classloader Hierarchy:

Following listing describes on the hierarchy of class loading from top to bottom as parent to child. First three Classloaders together typically shows the Java Classloading hierarchy and with the addition of the fourth Classloader (which has its own hierarchy) shows a typical Enterprise Application Classloader hierarchy.



Things to remember about Classloading:

1. Delegation – Always the current associated Classloader delegates the request to load a class to the immediate parent Classloader before attempting to load on their own. Delegation happens only when a particular class is not loaded in cache already.

2. Visibility - Classes loaded by parent Classloader are visible to all child Classloaders but not vice versa.



3. Uniqueness - Once a class is loaded by any of its parent Classloader, child Classloader in the hierarchy will never reload the class again.

4. Configurable - Most Application Specific Classloaders are configurable based on which the server automatically creates a hierarchy of Classloaders when an application (EAR) is deployed where root shared Classloader is created for loading bean libraries of EJB JAR, independent child Classloader are created for loading classes under the WEB-INF and WEB-INF/lib directories of WAR and for JSP classes for each web application. The parent of the hierarchy created is always System Classpath Classloader. The process of redeploying the application modules without server restart is termed as hot-deployment and is closely related to Classloading.

Tuesday, November 4, 2008

New features of Java 6 (Java Mustang/J2SE 6.0/JDK 1.6)

Core libraries features:

Features include changes to core libraries of java.lang, java.io, and java.util packages. It includes the following changes:

java.lang package features: New methods have been introduced in the java.lang.String and java.lang.Math class.

· Empty String check: Includes isEmpty method for checking empty String.

Usage: Use myString.isEmpty() instead of using myString.equals("") or myString.length==0 where myString is of type java.lang.String.

· Floating point enhancements: Includes methods like scalb, getExponent, copySign, nextUp, nextAfter for making floating point comply with IEEE 754 standard.

java.io package features: New methods have been introduced in the java.io.File class.

· File manipulations: Includes methods like getFreeSpace, getUsableSpace, getTotalSpace, serReadable, setWritable, setExecutable, and canExecute for manipulating free space (with and without account restriction imposed by OS), total space, read/write/execute permission details on the drive operated.

java.util package features: New methods have been introduced in the java.util.Arrays class.

· Array reallocation: Includes methods like copyOf and copyOfRange (both comes with 10 overloaded methods to support primitive types and objects) for copying the elements of one array to another array providing scope for resizing/reallocating.

Usage:

..

// Create array of length 5

int intArr1[]={1,2,3,4,5};

// Copy all elements of array created earlier and reallocate the size with additional one element

int intArr2[]=Arrays.copyOf(intArr1, 6); // intArr2 will hold 1,2,3,4,5,0

// Copy elements from 2nd position of array created earlier and reallocate the size with two additional elements

int intArr3[]=Arrays.copyOfRange(intArr1, 2, 7); // intArr3 will hold 3,4,5,0,0

..

· New Collection framework types: The Java Collection framework adds few new interfaces and implementations