By: Hemal Pandya, Sybase Inc.
Java 5, codenamed Tiger during development, was released in late 2004. Though it has been around the corner for about two years now, many organizations are only now beginning to adapt it for production following the trend of 'stay-one-step-behind', to ensure stability.
Now that Java 6 has been released and Java 5 dot releases are into double digits, this is the ideal time for move. This way, your manager knows that if something does not work you cannot blame Sun!
Next in the series: Exploring Varargs
Java 5 adds several new features to the Java language and the libraries. They were designed to make a programmer's tasks easy and in some cases were long standing demands from the Java community. These features span from very simple (printf) to very complex (generics).
In this series we will explore some of the features Java 5 in depth. We will start with the simpler ones before moving on to complex and arcane ones. We will use lot of code examples to see how these impact the new methods we write and JDK methods we use.
I will start with an overview of the new features in Tiger and explore one of these in this article. I look forward to your comments and queries.
Oh yes, just in case you haven't figured that out yet, Java 5 also changes the naming scheme for Java. Some time ago Sun dropped the '2' from the name and introduced a concept of product version, which dropped the initial 1 from the number. So the developer version 1.5.0 is the product version 5.0 and developer version 1.6.0 is product version 6.0, or Java 6.
New features of Java 5:
Varargs- More formally known as variable arity method. These are methods that take variable number of arguments.
Autoboxing/Unboxing- Boxing is the automatic conversion of a value of primitive type - boolean, int, float etc to the corresponding values of reference types - Boolean, Integer, Float, etc. Unboxing is the reverse, automatic conversion of a value of reference type to the corresponding primitive types. These are especially useful and perhaps necessary with the introduction of generics.
Typesafe Enums- provide the benefits of type-safety for a set of named constants, replacing the traditional method of using "public static final int". Enum introduces one more keyword to create a type.
Static Import- is the syntax for importing constants from one type (class or interface) into another. They provide an alternative to "Constant Interface" - an interface that declares several Objects and whose sole purpose is for other types to implement (or extend) it to get an access to those constants.
Generics- primarily provide compile-time type-safety for collections. They provide a way to declare a Vector of String (or Integer, or any user defined type) instead of the current Vector of Objects. When you retrieve a value from this Vector, you - and the compiler - know it is a String. No down-casting is required. They are similar to the C++ templates but significantly different and have such subtlety that great care must be taken in understanding them. They are by far the most complex of the concepts in Java 5.
Enhanced 'For' loop- is a way of accessing the elements of a collection (or array) that is simpler and less error-prone then Iterator or index variable.
Annotations-are type of comments with a special syntax - they start with '@' sign. Like comments they have no bearing on the run-time behavior of the code; they are used by different tools for different purposes. For example, the annotation @SuppressWarnings tells the javac compile to suppress reporting some or all warnings.
Printf- is finally introduced with Java 5. It is somewhat more elaborate then the C version. It comes in the form of PrintStream.printf, which allows calls such as System.out.printf. It is actually a convenience method that uses method format of the new class Formatter also introduced in Java 5. Scanner, also introduced with Java 5, is to input what Format is to output; it allows formatted input. Both these classes are in package java.util.
BigDecimal now has support for fixed-precision floating-point computation. JSR13.
Concurrency are a set of utilities that provide much needed functionality for developing concurrent programs. They derive from a set of classes originally developed by Doug Lea, who also headed theJSR166.
Collection classes have been genericified, that is, they are now Generic classes, providing type safety. Other changes that utilize other new features are Queue, BlockingQueue, ConcurrentMap, EnumSet and EnumMap. Besides that, there are special purpose copy-on-write List and Set implementations.
JAXP makes the work of JSR206 part of JDK. It provides basic facilities for working with XML documents.
getEnv is no longer deprecated.
These are just some of the changes; a simple search for identifying things introduced in 1.5 finds 1197 matches in 625 files!
A quick look at some other interesting changes
Instrumentation is provided as class in package java.lang.instrument, but I don't see this as a change in Base Libraries. It provides a mechanism for writing agents in Java that can instrument the running program. These can be used for writing tools like profilers or Aspect J in Java itself.
Monitoring and Management of the running JVM and the underlying operating system is now possible using the classes in java.lang.management. It can be used through jconsole, a graphical tool, the javax.management API and even SNMP.
APT is the command line Annotation Processing Tool, which allows the user to supply Annotation Processors that the tool invokes to process the annotations in the code. This functionality will eventually become part of the Java compiler javac.
Pack2000 provides a new compression format that reduces the sizes of JAR files.
Java Web Start includes several changes that enhance deployment and execution.
Desktop Application support has been enhanced, with new features in AWT and Swing and in media API for image i/o and sound.
Other changes required for completeness, such as serialization support for Enums, reflection support for Generics, Enums and Annotations, etc.
Watch out for the next article in the series that talks about Varargs in detail
Eco-friendly IT process not only makes a good environment sense, but also a very good business sense. Join us in this initiative that protects nature and your business.
know more..