I just got back from a "Best of Java Puzzlers" presentation that was held at Google’s NYC office.

The Google NYC Office is currently located in Times Square (1440 Broadway) but they will be moving downtown in July 2006.

The Java Puzzler presentation was given by world renowned Java Expert Josh Bloch.

Josh is an entertaining speaker.

He quickly walked through 8 trick Java questions.

It’s interesting stuff for Java developers especially Java Certification folks.

You can download all of the puzzlers here: http://www.javapuzzlers.com/

This is one of them.

public class Elementary {

public static void main(String[] args) {

System.out.println(**1**2345 + 5432**l**);

}

}

The casual observer would say that the answer is: 66666

Surprisingly, the answer is: 17777

The trick is the first digit of 12345 and last digit in 5432l. The last digit is really a lower case L.

So the compiler interprets 5432l to be a Long data type containing 5432.

12,345 + 5,432 = 17,777

This is another one:

public class LongDivision {

public static void main(String[] args) {

final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;

final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;

System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);

}

}

Once again, the casual observer answer should be 1000.

However, due to a data type overflow, the real answer is 5.

The problem is the MICROS_PER_DAY calculation (24 * 60 * 60 * 1000 * 1000).

Internally it uses a regular int data type which causes an overflow.

To fix, just need to append ‘L’ to any one of the number literals:

final long MICROS_PER_DAY = 24**L** * 60 * 60 * 1000 * 1000;

Afterwards, I asked Josh if he agreed with Bruce Eckels comments that the Erasure approach to generics was a mistake.

He quickly stated that he completely disagrees with Bruce Eckels.

Josh believes that Erasure is a great solution.

It’s the best way to solve the "upward compatibility with existing code" problem.

I’ll dig a little deeper into **Erasure** versus **Reification** (Microsoft’s approach) in another blog entry.

In the mean time, Josh pointed me to Neal Gafter’s great blog entry about it here.