Tuesday 27 May 2014

A Java Refresher - some pain points and annoyances

With the recent release of Java 8 my new project team has chosen to take a look into Java as a viable alternative to the existing default language choice of Scala.

(In the interests of full disclosure we did originally opt for Scala, with a few caveats and a recommended reading list for the features to avoid or use sparingly).

I had already started reading Functional Programming in Java 8, but I think now is a good opportunity to look back over the features of the language and consider where there are particular strengths and weaknesses.

This post will only focus on Core Java - not Java Enterprise Edition, Swing or any frameworks.
  • Labeled break / continue
    • These expressions are the closest you can get to having a goto in Java
  • Checked Exceptions
    • I haven't quite convinced myself about this one yet, but I have a particular dislike for having to wrap a call with a try / catch block where the catch block only contains a comment like // can never happen
    • All too often exceptions are used to control flow for scenarios that aren't truly exceptional
  • Reflection
    • Generally prevents the compiler from having static access to the relationships between classes.
    • On the flip side, static analysis tools can be expected to make use of reflection.
  • Local classes
    • Did you know that you can declare a class inside any block?  Not just anonymous classes, but even fully fledged named classes.
  • instanceof
    • Whenever I find myself introducing an instanceof check in my code I feel like there must be a better way.
  • Restrictions on Generics

Runtime gotchas
  • Memory allocation
    • Out of memory exception - particularly PermGen space, and particularly when reflection or dynamic class loading is involved - before Java 8.
  • Garbage collection
    • Stop the world - collecting large accumulated garbage while blocking other progress.

Sorry for the lack of substance, but sometimes this blog exists as a checklist of reminders for myself.  I haven't Googled a problem and reached my own blog yet, but I can imagine it will only be a matter of time.

No comments:

Post a Comment