One of the people I enjoyed seeing at Science Foo Camp this year was Joshua Bloch. Josh is a Java Junkie [1,2,3] and software engineer at Google. When he wasn’t playing harmonica around the foo camp fire (see picture right), he was giving interesting talks about optical illusions, some of which can be found in his book Java Puzzlers. So I bought the book, and have been doing a puzzle a day to keep the doctor away. Most of the puzzles in this book are short Java programs that behave in ways you would not expect. The one below is a nice example:
public class Indecisive { public static void main(String[] args) { System.out.println(decision()); } static boolean decision() { try { return true; } finally { return false; } } }
What does this program do? Return true
or false
? Perhaps it does both or something else completely? Does it even compile? Can’t decide? Welcome to public class Indecisive…
References
- Joshua Bloch and Neal Gafter (2005). Java Puzzlers: Traps, Pitfalls, and Corner Cases (isbn:032133678X) Addison-Wesley
- Joshua Bloch (2006). How to design a good API and why it matters OOPSLA ’06: Companion to the 21st ACM SIGPLAN symposium on Object-oriented programming systems, languages, and applications, 506-507 DOI: 10.1145/1176617.1176622
- Neal Gafter (2008). Is the Java Language Dying? Neal Gafter’s blog: Thoughts about the future of the Java Programming Language.