Tuesday, November 30, 2010

Java Puzzlers at Strange Loop

Just came across the latest Java Puzzlers presentation by Josh Bloch and Bob Lee at strange loop earlier this year.  7 new puzzlers to make your brain hurt, and your jaw drop a little bit when you see the solutions and morals.

Spoiler alert! If you like puzzlers stop reading now!

This is my take on it:

Do not use raw types, the compiler will resolve to the best match for the type and cast automatically, and this can get you into trouble with overloaded methods.

Choosing the right types might not be enough; look for quirky differences among constructors for those types, such as BigDecimal(double) and BigDecimal(String).

Avoid using Varargs and arrays together, and prefer collections to arrays, especially in APIs.

Be careful with catastrophic backtracking when using regular expressions (not only in Java!).

I do not know enough about map.entrySet() and HashSets, so I got lost in that puzzle. I will have to watch it again. But in any case:
Iterating over entry set should be done with care.

When in doubt, use a larger type to avoid overflow problems.

Leading 0s transform the number in octal literals. Use with care!

And lastly, Do not ignore compiler warnings; they can get you out of trouble before you hit it at runtime!

Josh Bloch mentioned that findbugs catches 5 of the 7 pitfalls, so if you were thinking about giving it a go, this is a good reason to get you started!

1 comment:

  1. Please continue to write more because it’s unusual that someone has something interesting to say about this. Will be waiting for more…
    http://vpmarketingondemand.com

    ReplyDelete