Posted by: terryhowe | September 28, 2009

Java 5 Features That Matter

There is nothing new about Java 5, but I wanted to create a list of the features that I thought were useful in that release. There aren’t many I care about.

  1. Generic Types
    Generic types in Java 5 are basically just like templates in C++.  They give you more compile time type checking and you can avoid doing all the type casting.  It also makes it a lot less likely to have runtime exceptions.  An example of the ArrayList with generics:

       ArrayList<Integer> list =  new ArrayList<Integer>();
       list.add(0, new Integer(42));
       int total = list.get(0).intValue();
  2. Autoboxing and Auto-Unboxing of Primitive Types
    You can now add primitive types and get primitive types from containers without explicitly making a class type. Convenient.

       ArrayList list = new ArrayList();
       list.add(0, 42);
       int total = list.get(0);
  3. Enhanced for Loop
    In Java 5, you no longer need to explicitly create an iterator to loop through a container. The code is much more readable when writing loops.

       ArrayList list = new ArrayList();
       for (Integer i : list) { ... }
  4. Reference
    http://java.sun.com/developer/technicalArticles/releases/j2se15/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: