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:

  1. import static java.lang. Integer.*;
  2. 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:

  1. ArrayList<String> arrayList = new ArrayList<String>();
  2. arrayList.add("Testing");
  3. 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:

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

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):

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

Enums

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

  1. enum BookName { RAPID, JAVA, DEVELOPMENT };
  2. for (BookName bookName : BookName.values())
  3.   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:

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

The code that calls our demoVarargs method looks as follows:

  1. 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值