Our Devoxx Java Puzzler

Jack Shirazi
May 17, 2018 · 4 min read

This year Expedia Group sponsored Devoxx UK and I joined the Expedia booth as one of the representatives from Hotels.com (part of Expedia Group). As part of our booth, we created a Java puzzler which had a £200 holiday voucher for the winner (drawn from the correct answers to the puzzler). Immediately below is the puzzler. After the puzzler I’ll discuss the answers given, so if you want to have a go at the puzzler, avoid reading past this first code section, that is until you want to see the proposed answers.

We tried this out on some developers before the event, and they found it challenging. And from the conversations we had at Devoxx, developers there also found it challenging. While at the booth I grabbed Mark Reinhold, Chief Architect of the Java Platform (here with me using our photobooth at Devoxx with a nice fishing marina he selected as our background), and it took him a few minutes to get a valid (and indeed the simplest) solution — mainly because he was thinking of esoteric answers before he finally said “oh, it’s trivial!”

The simple solution

The simplest answer we were expecting was to define your own class called System in the same package as the PrintGoodbye class, and include that in the classpath eg

Yes, as simple as that. When I pointed this out to most developers, I could see them metaphorically facepalm themselves.

Other solutions

We had many suggestions of using very esoteric options like editing the compiled bytecode to replace the string “hello” with “goodbye” or redefine the Unicode charset to have “hello” come out as “goodbye” and point the JVM at those. I don’t know if these would work, but no one provided a workable set of instructions to try them out so there weren’t any of those entries for me to test.

We also had people suggesting defining a String class in the same package as the PrintGoodbye class, and this seems like it should work similarly to the System class definition above. But it doesn’t. Because the Java language specification requires the JVM entrypoint to be a quite specific signature:

and if you create your own String class in the expedia.puzzler package, the PrintGoodbye class uses that String class instead in the main() method including for the parameter. That changes the main() signature and consequently that main() is no longer recognized by the JVM as a valid entrypoint for the program. It does compile, but if you try to run that solution you would get an error “Error: Main method not found …”. So it’s an incorrect answer.

We also had several solutions redefining the line.separator -Dline.separator=”\b\b\b\b\b\bgoodbye\n”. I thought that was quite clever. But sadly it didn’t work. I ran it and the output was “hello\b\b\b\b\b\bgoodbye\n”. It might have worked on some system command lines, but I tried all solutions in a Linux shell and a Windows command prompt, and it didn’t work on either for me.

Finally we had technically correct solutions that you might not consider useful, but they did work when I tested them so we considered them correct and entered them in the prize draw. These were three types:

  1. Create your own launcher class as the main class and put that in the [OPTS] (we forgot to rule that out in the instructions so we decided we couldn’t say it was wrong). The class needs to take the PrintGoodbye class as an argument, then first print “goodbye” and then call PrintGoodbye while redirecting the output from PrintGoodbye (it’s wrong if you don’t redirect the output, since then the output is “goodbye\nhello”). Just ignoring the PrintGoodbye argument would be wrong since the instructions specifically require the class to run.
  2. Load your own classloader using the java.system.class.loader property which redefines the main of PrintGoodbye to print “goodbye” (dubious since the class is altered, but I decided it was valid. And quite inventive)
  3. And finally a very cheeky

which did actually work on both my test shells (the “hello” is just dumped by the echo command; both the pipe and the echo command work on both Windows and Unix. A similar solution using a redirect instead of pipe didn’t work because /dev/null doesn’t work on Windows).

It was fun creating the puzzler, fun discussing it with the developers at Devoxx and fun looking at the solutions suggested. All in all, a fun experience.

The Hotels.com Technology Blog

Public blog for the Hotels.com Technology team.

Jack Shirazi

Written by

Working in the Performance and Reliability team. Also: founder of JavaPerformanceTuning.com; Java Performance Tuning (O’Reilly) author; Java Champion since 2005

The Hotels.com Technology Blog

Public blog for the Hotels.com Technology team.