Tiger (Java 5.0) Generics, auto (un)boxing and new For loop

1

Knowing that I will not be the only one posting on these features in the Java Tiger release here are some observations I made while running some tests.
I created an example showing Generics, auto (un)boxing and the new for loop in one little program.
I actually used the Netbeans IDE 4.0 to create it. At (my) first glance Netbeans looks like a very good IDE with a lot of features also present in Eclipse.

<code> public void runTest() {
        // Generics: only Integers allowed
        ArrayList&lt;integer&gt; col = new ArrayList&lt;integer&gt;();
       // Math.random();
        for (int i = 0; i&lt;1000000;i++) {
          // autoboxing
           col.add(getRandom());
        }
        int total = 0;
        Integer oldValue = null;
        boolean allTheSame = true;
        // new For loop
        for(Integer s : col) {
            // auto unboxing
            total += s;
             if ( oldValue != null && oldValue != s ) allTheSame = false;
            oldValue = s;
        }
        System.out.println(total);
        System.out.println("Were are all the same " + allTheSame);
    }

   // method always returning randomly generated 1 ;-) 
     private int getRandom() {
         return (int)Math.ceil((Math.random() * 1));
   }
     </code>

Prompted by “In addition, the enhanced for construct is a compiler ‘trick’ and can actually end up being slower when used for large collections.” quote in the Tiger feature list, I tried measuring some performance. Using the auto boxing feature to add the radomly generated 1′s to the collection I noticed that all objects are equal in the collection (using the == operator).

If I use the “old” way

<code>
      col.add(new Integer(getRandom()))
</code>

to add the radomly generated 1′s , I get all different objects (of course).
This has of course quite an impact on the performance ( 511 ms vs 1132 ms )

If I now change my getRandom method so it returns a double and also change my ArrayList to a generic double implementation I get all different objects in both the old way and the new way (auto boxing).

For completeness the same method in pre 1.5 code

<code>
  public void runTestOld() {
        ArrayList col = new ArrayList();
        for (int i = 0; i< 1000000;i++) {
         col.add(new Integer(getRandom()));
        }

        int total = 0;
        Integer s =null;
        Integer oldValue = null;
        boolean allTheSame = true;
        for(Iterator i = col.iterator() ; i.hasNext();) {
            s = (Integer)i.next();
            total += s.intValue();
            if ( oldValue != null && oldValue != s ) allTheSame = false;
            oldValue = s;
        }
        System.out.println(total);
        System.out.println("Were are all the same " + allTheSame);
    }

</code></code>

I actually did not measure any significant differences between the old way of looping with an Iterator and the new For loop (yet).

Also check the new compiler flag -Xlint:unchecked . With that flag the compiler will tell you were you work unchecked

  <code>
C:TigerTestsrcGenericsTest.java:128: warning: [unchecked]unchecked call to add(E) as a member of the raw type java.util.ArrayList
         col.add(new Integer(getRandom()));
1 warning
  </code>

Personally I think generics, auto ( un) boxing and the new for loop will help Java Developers to write more concise and clearer Code.

Share.

About Author

1 Comment