Java Puzzlers

The complete name of this JavaOne 2007 session was Java Puzzlers, Episode VI: The Phantom-Reference Menace/Attack of the Clone/Revenge of the Shift. This technical session was presented by perhaps the most effective Java developer Joshua Bloch and fellow puzzler William Pugh. Joshua and William presented 8 short programs with curious behavior. This session was basically a what does this program print interactive question and answer discussion.

The puzzles where like Google interview questions, a bunch of brain teasers which intended to preach a few moral and teach a few tips regarding interesting behavior at the language and JDK level. For example here are some tips I took away from the session, use URI instead of URL because the hashCode and equals method in URL is broken. Don’t mix short, long, int primitives and objects because autoboxing with knock you out. Autoboxing happens, when you least expect it. JUnit does not support concurrency. Wrapped primitives aren’t primitives. Watch out for circular class initialization when you mix statics and constructors, remember static code is process from top to bottom before constructors. If an API is broken, wrap it. For API designers, don’t violate the principle of least astonishment. Math.abs doesn’t guarantee non-negative results, for example Math.abs(Integer.MIN_VALUE) because Integer.MIN_VALUE == -1*Integer.MIN_VALUE. Avoid mixing types in the ?: ternary operator. Use find bugs, acording to Joshua and William Find bugs warned all of the questionable behavior.

Lastly, William said that the “Collections API where designed for what is type-safe, not what is sensible.”

Technorati Tags: , , , , , ,