Recently Added Java Features


Let's begin with the Java Platform Standard Edition (JSE) Development Kit (JDK), because this is where we began our hands-on work back in Chapter 4, "Environment Setup: JDK, Ant, and JUnit."

JDK 1.5 (also known as JSE 5.0) introduced some new features that aim to enhance the Java languages. Let's look at some of these features here; if you are already familiar with these features, you can skip this section.

Backward Compatibility with JDK 1.4 for Time Expression

I intentionally did not use any of these new features in our sample application, Time Expression, because I wanted it to provide backward compatibility with JDK 1.4 in case your organization has not adopted JDK 1.5 yet. Also note that I used Eclipse's Window, Preferences, Java, Compiler option to maintain compatibility with JDK 1.4. This is a very handy feature, which I encourage you to investigate if you aren't already familiar with it.


The complete code for the features discussed next can be found in a file named DemoNewJavaFeatures.java in this book's code zip file. I will provide only brief descriptions here because the examples are simple, and ample documentation is available for these on the java.sun.com website.

Static Import

Since JSE 5.0, it is possible to use static members directly. For example, something like Integer. MAX_VALUE can be used as follows:

import static java.lang. Integer.*; System.out.println(MAX_VALUE);


Generics

In previous releases, we could insert any type of object in a class from the Collections framework; then we had to cast objects retrieved from the Collections framework. This provided somewhat unsafe operations because the compiler couldn't check for type safety. Now we can avoid this by doing something similar to what's shown nextnotice how I can simply use the get method without the need for casting:

ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Testing"); System.out.println(arrayList.get(0));


Furthermore, we add only objects of type String to this ArrayList. For example, the following code would cause a compilation error:

arrayList.add(new Integer(1));


Enhanced for Loop

The for loop has been greatly simplified since JSE 5.0. To iterate through a collection class, instead of using the old style, for (int i=0; i < c.size; i++), we can do the following:

public static void demoForLoop(Collection<Integer> c) {   // using new style for loop   for (Integer i : c)     System.out.println(i); }


This not only unclutters the code slightly but can also help reduce common errors caused by invalid checking of index variables (for example, the variable i in this example).

Autoboxing

This is a nice feature and in my opinion should have been supported in Java all along. As you might know, we cannot put primitive data types (for example, int) in collections classes. With this new feature, we can add a number into something like an ArrayList, as shown here, without the need to use new Integer(1):

ArrayList<Integer>list = new ArrayList<Integer>(); list.add(1);


Enums

The simple example provided next does not demonstrate the power of enums in Java:

enum BookName { RAPID, JAVA, DEVELOPMENT }; for (BookName bookName : BookName.values())   System.out.println(bookName);


The following description taken directly from the java.sun.com website does more justice to this feature:

"Java programming language enums are far more powerful than their counterparts in other languages, which are little more than glorified integers. The new enum declaration defines a full-fledged class (dubbed an enum type)... it allows us to add arbitrary methods and fields to an enum type, to implement arbitrary interfaces, and more. Enum types provide high-quality implementations of all the Object methods. They are Comparable and Serializable, and the serial form is designed to withstand arbitrary changes in the enum type."

Varargs

Varargs provide the capability to pass variable arguments in methods. Before, we had to do this using something like an Object array. Now, we can use ellipses(...) to do this, as shown here:

public static void demoVarargs(Object... args) MessageFormat.format(     "I''m working on {0}"     + " on {1}"     + " at {2} hours.", args);


The code that calls our demoVarargs method looks as follows:

demoVarargs("Rapid Java Development", new Date(), 1800);


Furthermore, JDK classes such as MessageFormat also accept variable arguments, as demonstrated in this example.

Other Features

There are other features (annotations, for example) and enhancements to the API and JVM that I have not covered here; however, details and tutorials on these can readily be found on the java.sun.com website. Furthermore, my simple examples do the new features justice. As we can see from Figure 10.1, Java is a huge platform; it is no wonder there are hundreds of books on Java and entire books on single subjects such as Java Security APIs, JDBC, and others.

Figure 10.1. Java Platform Standard Edition 5.0
(source: java.sun.com).


Also, at the time of this writing, J2SE 6 beta was announced, which provides new/enhanced security features, integrated web services, enhanced JMX support, improved GUI, and more. Visit the java.sun.com website for details.



Agile Java Development with Spring, Hibernate and Eclipse
Agile Java Development with Spring, Hibernate and Eclipse
ISBN: 0672328968
EAN: 2147483647
Year: 2006
Pages: 219

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net