Archive for April, 2006

Best of Java Puzzlers – Josh Bloch

April 19, 2006

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

Date/Time Phenom – 01:02:03 04/05/06

April 5, 2006

On Wednesday, April 5th, at 3 seconds after 1:02 am and pm the time and date will be…
     01:02:03 04/05/06

This event occurs every 1,000 years. Which reminds me of the famous Clock of the Long Now.
 
The "long now clock" is a 10,000 year clock that ticks once a year.
The cuckoo comes out every millennium.

At a previous startup company, I was fortunate enough to meet a guy named Bran Ferren.

Bran Ferren is a former Walt Disney Imagineer.
His company Applied Minds is a creative think tank shop.

In 2002, Bran Ferren built a vehicle called a Maximog that was shown at MOMA for a few weeks. 

He is friends with the famous Danny Hillis, creator of the Clock of the Long Now.
Bran Ferren and Danny Hillis are renaissance men.

MSNBC’s Michael Eisner interviewed Bran Ferren yesterday.

Be sure to listen to his comments about implantable devices:
=> "We are 25 years away from being wired all the time.".