Home of The JavaSpecialists' Newsletter

083End of Year Puzzle

Posted: 2003-12-26Category: LanguageJava Version: Dr. Heinz M. Kabutz
 

Abstract: 

 

Welcome to the 83rd edition of The Java(tm) Specialists' Newsletter. The year 2003 is now almost over, and before we say "goodbye" completely, I thought to slip in one more quick newsletter to test your Java skills :-)

Translations: We started converting The Java(tm) Specialists' Newsletter into some other languages besides English [although someone suggested that we should translate it into English as well ;-] We now have translations into Portuguese (Rafael Steil and Vanessa Sabino), Zulu (Mondli Mabaso), Polish (Daniel Kwiecinski), Romanian (Dikran Seropian), Catalan (Horaci Macias), Spanish (Horaci Macias) and German (Heinz Kabutz). The translations are available on our archive page.

NEW: Refactoring to Java 8 Lambdas and Streams Workshop Are you currently using Java 6 or 7 and would like to see how Java 8 can improve your code base? Are you tired of courses that teach you a whole bunch of techniques that you cannot apply in your world? Check out our one day intensive Refactoring to Java 8 Lambdas and Streams Workshop.

End of Year Puzzle

This time of year is called the silly season in South Africa. In Cape Town, as soon as December starts, and all the holiday makers arrive, the road works spring up all over the city. This exacerbates the traffic situation and gridlocks the roads. Maybe it takes 11 months to decide on the contractor, and then the work has to be finished before the end of the year?

So, seeing that it is "silly season", here is a little Java puzzle for your amusement, based on an idea that was sent to me by Steve Mabbort and Ben Halton. It is almost like a question you would find in the Sun Certified Java Programmer examination...

What is the output of the following Java snippet?

public class Puzzle1 {
  public int test() {
    int i = 1;
    try {
      return i;
    } finally {
      i = 2;
    }
  }
  public static void main(String[] args) {
    Puzzle1 p = new Puzzle1();
    System.out.println(p.test());
  }
}
  

There are several possibilities, so let's make a multiple choice out of it. To make it interesting, why don't you click on the [Select] link next to the answer that you would think to be correct? I will then tally the answers and post the result in the next newsletter.

  1. The code does not compile
  2. The code compiles, but throws an exception
  3. The code prints "0"
  4. The code prints "1"
  5. The code prints "2"
  6. None of the above
  7. All of the above

After you have sent me your answer, you should try it out and see whether you were correct.

Let's change the puzzle a bit. Now it is not so obvious anymore.

public class Puzzle2 {
  private int i = 1;
  public int test () {
    try {
      return i;
    } finally {
      i = 2;
    }
  }
  public static void main(String[] args) {
    Puzzle2 p = new Puzzle2();
    System.out.println(p.test());
  }
}
  
  1. The code does not compile
  2. The code compiles, but throws an exception
  3. The code prints "0"
  4. The code prints "1"
  5. The code prints "2"
  6. None of the above
  7. All of the above

To understand how and why this is so, I would like to suggest that you disassemble the java classes and read the byte code. You can do that with the command:

    javap -c Puzzle1 Puzzle2
  

That's all for this year, see you again in 2004 :-) I wish you a prosperous 2004, both physically and spiritually :-)

Kind regards

Heinz

 

Related Articles

Browse the Newsletter Archive

About the Author

demo

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

Nobody ever wants to call a Java performance consultant, but with first-hand experience repairing and improving commercial Java applications - JavaSpecialists are a good place to start...

Threading Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.