Running on Java 24-ea+24-2960 (Preview)
Home of The JavaSpecialists' Newsletter

083End of Year Puzzle

Author: Dr. Heinz M. KabutzDate: 2003-12-26Java Version: 1.1Category: Language
 

Abstract: Two easy Java puzzles to test your understanding of try-finally.

 

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.

javaspecialists.teachable.com: Please visit our new self-study course catalog to see how you can upskill your Java knowledge.

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

 

Comments

We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

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

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

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

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...