Browse DevX
Sign up for e-mail newsletters from DevX


J2SE 1.5: Java's Evolution Continues : Page 2

J2SE 1.5 (codename: Tiger) epitomizes Java's ongoing evolution and adaptation to the emerging needs of programmers and their applications. With features such as generics, autoboxing, and enhanced for loops, it makes Java code easier to develop and read.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Autoboxing and Auto-unboxing
Tiger converts primitive types to wrapper types and vice versa. For example, take a look at the following code found in AutoBoxExample.java:

public class AutoBoxExample { public static void main (String args[]) { handlePrimitive(new Integer(20)); handleWrapper(30); } // note: method would normally only handle primitives private static void handlePrimitive(int prim) { System.out.println(prim); } // note: method would normally only handle wrappers private static void handleWrapper(Integer wrapper) { System.out.println(wrapper); } }

Note that you can pass an integer (primitive) where an Integer (wrapper object) is expected and vice versa. The Tiger compiler, which performs the necessary conversion for you, makes this possible. It assures that the proper type gets passed into your method, converting from primate to wrapper object or wrapper object to primitive as needed.

Enhanced For Loops
Another major language change in Tiger is enhanced for loops. The newly designed for loop is intended to allow easier integration through collections. In previous versions of Java, you had to use an iterator, as shown in the iterateOldWay method of the following class:

import java.util.*; public class EnhancedForLoopExample { public static void main (String args[]) { ArrayList arrayList = new ArrayList(); arrayList.add("Apple"); arrayList.add("Banana"); iterateOldWay(arrayList); iterateNewWay(arrayList); } // use of iterator required in previous versions of Java public static void iterateOldWay(Collection coll) { for(Iterator i = coll.iterator(); i.hasNext();) { String grab = (String)i.next(); System.out.println(grab); } } // new way to iterate through collection using enhanced for loop public static void iterateNewWay(Collection coll) { for (Object obj : coll) { System.out.println((String)obj); } } }

Take a look at the iterateNewWay method. Note the new for syntax. You are no longer restricted to providing pre and post conditions in your for statement. Your new syntax loops through each element of your collection.

To save time, the generics concept and enhanced for loops can be used in tandem. Notice how the following class leverages both concepts:

import java.util.*; public class EnhancedForLoopExampleWithGenerics { public static void main (String args[]) { // create an ArrayList and restrict contents ArrayList<String> stringList = new ArrayList<String>(); // add a couple of Strings to the ArrayList stringList.add("Apple"); stringList.add("Banana"); for (Object obj : stringList) { System.out.println(obj); } } }

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date