Jan 032005
 

In part one of this series of articles, we had a look at generics and how they can make a difference to the way you code Java.

In this second part, I will cover two major features introduced in
J2SE 5.0 , ?Enhanced For Loop? and ?Autoboxing/Unboxing ?.Let?s start
with the enhanced for Loop.


Enhanced For Loop

First of all, have a look at the traditional approach of writing a for loop to iterate over the arrays/collections. Then we can understand why the new for loop is termed as Enhanced.

Listing 1

List mylist = new ArrayList(); 
mylist.add(new Integer(10)); 
mylist.add(new Integer(99)); 
for (int i=0; i < myList.size(); i++) { 
  Integer mynum = (Integer)mylist.get(i); 
  System.out.println("mynum>> " + mynum); 
} 

In the above code we can see that to iterate over an Arraylist we use an Iterator. to iterate over mylist.

Then in for loop we cast each object from mylist into an Integer

New Approach:

Listing 2

List mylist = new ArrayList(); 
mylist.add(new Integer(10)); 
mylist.add(new Integer(99)); 
for(Integer mynum:mylist) { 
  System.out.println("mynum>>"+mynum); 
}

Listing 2 shows the new changed syntax of for loop. In the above code ArrayList has been defined and two elements have been added to it. In blue note the new for loop.

Listing 3 will explain the structure of new for loop.

for (declaration : expression) 
{ 
  statement 
}

In Listing 3 declaration refers to just a variable. The type of this variable should the same of which elements are stored in the list/collection.

Expression can be a variable as shown in Listing 2. (e.g. mylist). This expression should be the one on which the iteration can be performed or that can be iterated.

The main thing in all this stuff is how the iteration can be performed on an ArrayList or Collection without taking size of a collection and performing a for typical loop on it.

Listing 4

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

Listing 4 shows that ArrayList extends AbstractList. Have a look at Figure 1 and the entire thing will be clear.

Figure 1

As shown in Figure 1 above the new implementation of the ArrayList makes all the difference. And provides the

enhanced things. New ArrayList class implements the methods from the List interface and as List extends Iterable, every ArrayList is an Iterable. (See Figure 1)

So when we write new for loop, compiler automatically generates a necessary looping code and with the help of generics the additional efforts required for casting (As shown in Listing 1 above) are eliminated.

This shows that the new enhanced for loop provides a hassle free solution for data manipulation.

Auto-Boxing/Unboxing

Auto-boxing/Unboxing is a small but important feature introduced in J2SE 5.0.

Auto-boxing/Unboxing is a clean and easy approach to converting primitive data types to their respective wrapper classes.

Listing 5

List mylist = new ArrayList(); 
mylist.add(0,new Integer(10)); 
mylist.add(1,new Integer(99)); 
int mynum = ((Integer)mylist.get(0)).intValue(); 

Listing 6

List mylist = new ArrayList(); 
mylist.add(0,10); 
mylist.add(1,99); 
int mynum = mylist.get(0);

Listing 6 shows that now we can add the elements to collection without specifying its type. But this can be possible when the type of element is stated when declaring the collection.

See line List <Integer> mylist = new ArrayList <Integer>(); in Listing 6 above. You see Generics helps us here.

Conclusion

Thus we see how enhanced for loops, auto boxing and unboxing simplify Java development. In the next article we will look at MetaData / Annotations and Enumerated Types.

Chinmay Ogale

The author, Chinmay Ogale can be reached at (chinmayogale AT gmail)

The following two tabs change content below.
  • Guest

    I just loaded open office and I think it’s great. :grin

  • Guest

    The only difference is in the syntax. AutoBoxing does not give any performance improvements over using wrapper classes as AutoBoxing is using the wrapper classes behind the scenes.

  • Guest

    auto boxing should have been introduced long back. having to work with wrapper classes in such cases was always an unnecessary overhead.