Interesting application of recursion in Java