Jun 22, 2004

Knowing how to use it...

Although not yet available on-line, the 90th issue of The JavaTM Specialists' Newsletter deals with two things I wanted to talk about.

In the newsletter he briefly comments about The Psychology of Computer Programming, and I agree. It is one of those many books that I have begun but have yet to finish, but you can just pick it up, and continue reading, have a good laugh to yourself, and put it back down.

To me, the more interesting fact is the issue that this and The Mythical Man Month have both been talked about recently, or really, always being talked about. Recently Ed Willis at O'Reilly has read and provided his insights into this classic, which was also discussed over at SlashDot. Both of these have some good points, and its always fun to go back in time, to before I was born :-).

In the second part of his discussion, Dr. Kabutz discusses his observations with Java 1.5's new Auto-Boxing feature. Although I have been keeping up-to-date on its features, I have not really given them a whirl yet.

I was actually quite pleased to see the perl-like foreach semantic for a for (I know foreach does not originate with Perl, but is the first language I learned with this feature). To iterate over each element in a given set g of type G, you can simply code for ( G element : g ) { ... } . I am looking forward to seeing something like this in C++, where its foreach() template is not as inlined and compact as this.

In his normal style, Heinz looks further than the language feature, and reports on the performance of this feature. According to his research, the above code runs comparable to the full code that we used to have to write.

There is always a but, isn't there? The above discussion was based on a Container, and Containers in Java must be of a class-type, whereas it is sometimes convenient to have a primitive type (int instead of the class Integer); some people do not consider Java a true Object-Oriented language because of its inclusion of, and in some cases, the special treatment of, primitive types. Some people may think that the Autoboxing feature addresses this complaint...

Anyhow, if your container, C contains instances of Integers, then it could be tempting to use the loop, for ( int element : C ) { ... }, however, Heinz reports that this causes the application to be 20 times slower.

Its amazing how such a simple change can result in such a large difference in processing time. This is similar to the C++ idiom for incrementing iterators using the postfix operator, since it does not return a copy of the object. I am sure that a new edition of Effective Java will provide us with many new techniques to avoid like this one.

Knowing the features is only part of knowing the language; knowing how to use them and when to use them is another.

Filed In