Friday, 15 March 2013

Natural Progression of Java for Statement

Introduction


I find it fascinating the way Java is being developed, and accrues different new features from other "newer" programming languages.

One of the things that struck me forcefully recently, is the progression of our beloved for Statement. So forcefully, that I had to make a blog about it.

Output of the programs listed below is always:
James Gosling is 57 years old.
Charles Babbage is 221 years old.
Alan Turing is 100 years old.
Donald Knuth is 75 years old.
Edsger Dijkstra is 82 years old.
Anyone interested in the Person.java class used, can find it here.

Version 1

This is the way we did it back in the old days. A counter for 0 until we reached the size of the list we wanted to iterate over. And every time we had to cast what we got out of the list, as the list contained Objects.

Version 2

Finally! A Collections framework! With default naming, so we could always more or less assume what the exact Contract was that the Collection adhered to, once we knew the name of the Collection used.

Version 2.5

Yay! No more casting, no more 'counters' and we can add a variable number of arguments to some of our collection framework methods.

Version 7

Small change, due to project Coin. We have a simpler notation for Generics.

Version 8

Lambdas, here we come!

Conclusion

The for-loop, while still a very important tool in the arsenal of the Developer, seems to have been relegated to the internals of the Frameworks. I would not be surprised if the for-loop will become used less and less.

Small note

Whilst trying to get Lambda functions to work, I made the mistake to download the OpenJDK snapshot from Oracle that does not actually contain Lambda syntax yet. Try [1].

References

The for Statement
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
Maurice Naftalin's Lambda FAQ
http://www.lambdafaq.org/
JDK 8 Features
http://openjdk.java.net/projects/jdk8/features
Java 8: The First Taste of Lambdas
http://zeroturnaround.com/labs/java-8-the-first-taste-of-lambdas/
[1] Java™ Platform, Standard Edition 8 Early Access with Lambda Support
http://jdk8.java.net/lambda/
Java Version History
http://tech-my-talk.blogspot.in/2013/03/java-version-history.html

No comments:

Post a Comment