Search This Blog

Wednesday, 22 September 2010

A java puzzle

I like coming up with these things once in a while. They exercise the mind, make people think and occasionally someone comes up with another method or two of solving the thing I hadn't considered.

So, here we go.

You've been presented with the following class written by your boss, which is meant to print "goodbye" out to the console. However, as you can clearly see the following code actually prints "hello". Your boss has asked you to rectify this problem somehow, but his code is perfect and it must be a bug in the JDK (of course).

In fact, he's already compiled the thing and it's ready to go, so he's said the only thing you're allowed to do is to add / modify / change class files in the bosscode package before it's released. Change whatever classes you want, as long as it's not Main.class (that's the perfect one!)


package bosscode;

public class Main {
public static void main(String[] args) {
new java.lang.Runnable() {
public void run() {
System.out.println("hello");
}
}.run();
}
}


Good luck - I'll post my solution / explanation in a few days time if anyone attempts it!

No comments:

Post a Comment