Advanced Java

As I think I noted recently, I’ve been doing study towards a Sun Certified Java Programmer (SCJP) certification. It is progressing well, and I’m learning a lot. I really should publish the study notes that I’m hand writing as I learn new things some time…

Anyway, I thought I’d link to a few documents that I have read recently that are quite interesting.

The first document discusses Java generics, introduced in Java 5. Most people get the basics, but there are some interesting issues with generics such as erasure, wildcards, and generics inheritance. This is a very good article to read if you are programming in Java 5 or later (which you should be if at all possible!).

The second article is shorter, but discusses convariant return types, and how Java 5 changes the rules compared with earlier releases. Once again, very interesting if you wonder what the rules are when it comes to overriding methods in Java 5 and later.

Unfortunately I haven’t been keeping track of earlier documents, so I have no more links – but I’ll try to keep posting any interesting documents here in the future.

Java Exception Gotcha

I’ve been doing a bit of study for the Sun Certified Java Programmer exam recently, and just studied a piece of code which I was tricked by, so I thought I’d post it here.

When doing exception handling in Java, I always thought that a return statement prevented a finally { } block from being called. In other words, I assumed the output from the following code snippet should be “exe1”, not “exe1 finally”.

In actual fact, despite the return statement in the catch block, the finally method is still called prior to the method returning. As should be obvious, the ” what now?” message is never printed in this instance.

So, in summary, it’s reassuring to know that the finally block is called even when an explicit return statement is added – it means things like DB connections will always be closed. I always was a little nervous about return statements in try {…} catch {…} blocks, but now after reassuring myself it seems there is no need for this.

Java Performance Myths

Just a quick link to an interesting post on The Register here that discusses some ‘myth busting’ by two Google employees on the common myths surrounding the Java runtime/language. Very interesting reading, and a nice followup to my previous post. The The Register post links to this blog post which then links on to this interesting (powerpoint) slide deck.

In my day-to-day use of Java, I find it to be very performant, so it is quite unfair the stigma that is attached to it, that I presume arises due to the Java of yore – i.e. mid 90’s Java. As noted in the links above, each Java release (we’re presently at Java 1.6) achieves a very good performance boost over the previous release. As noted in the previous post, there is however very easy ways to ruin the performance of (particularly) AWT/Swing user interface applications, given the necessary use of threads, and the lack of understanding that many developers have generally towards threading, and specifically towards Java GUI threading.

Books I need to buy

This post is to (geekily) proclaim my excitement at the news of a second edition of Effective Java being released to publishing. This book is an excellent book for advanced Java developers wanting to learn the little nuances of the language. I would recommend anyone keen to learn more about Java to buy it, I certainly will be buying the 2nd edition. Apparently it is completely rewritten to focus on the features in the latest Java 5 and 6 releases.

The other book on my programming to buy list includes Java Puzzlers, which is a question and answer style book that really tests your understanding of Java nuances.

The other book, that I’ve already discussed in this blog, and highly recommend is Java Concurrency in Practice. I really found this book a great book to discuss the new concurrency features in Java 5.

The common feature between all these books? Joshua Bloch. I swear I don’t buy books because of his name being on them, but I think it can be accepted that any book that does grace his name is worthy of reading if you are a Java developer.

Finally, don’t worry, those Amazon links aren’t there for me to get advertiser revenue (or any commission), so click away without any feeling of being duped. If you know any good Java books, please let me know!

Mystery of Accessibility in Local Inner Classes

In Java, I’d always known that whenever I wanted to pass a local variable into a (possibly anonymous) inner class without any getter/setter code I had to declare the variable as final, or alternatively it had to be an instance variable of the parent class.

If you just read that sentence and had no idea what I said, please feel free to skip the rest of this post.

What I didn’t know was why this was necessary. Clearly the assumption is that Java does some trickery, but needs the variable to be a constant value. Then I read this blog post and learnt that essentially that is what happens. Perhaps more interesting is that inner classes are only handled at the compiler level, in other words the JVM is completely oblivious to their existence.

If you are a Java dev and have a few minutes spare, this blog post is very interesting. I’m always interested in learning new aspects of the Java language/runtime, and I have a feeling I’m going to need to find a book on Java bytecode sooner or later.