Best of Java Puzzlers – Josh Bloch

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(12345 + 5432l);
    }
}

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 = 24L * 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.

Advertisements

Tags: ,

2 Responses to “Best of Java Puzzlers – Josh Bloch”

  1. computer mouse handling illustrations Says:

    I love your blog.. very nice colors & theme. Did you make this website
    yourself or did you hire someone to do it for you? Plz answer back as I’m looking to design my own blog and would like to find out where u got this from. many thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: