Branching Without an if, A Java Puzzle

Branching Without an if, A Java Puzzle

I returned home late the other night, and saw this tweet in my timeline:

He added a clarifying note that said the ‘?’ was also broken.

For those of you who don’t know Marc, he is the co-lead of the Eclipse C/C++ Development Tools and all round good guy. I was pretty sure his keyboard wasn’t actually broken and he was basically asking: How can you write a branch statement in Java without an if.

My first thought was to use another branching construct, and the while loop was the first one to pop into my head:

<pre lang="java">
int bar(Object o) {
  while(o == null) {
    return 0;
  }
  return 1;
}
</pre>

However, since I couldn’t sleep I started thinking of other ways to solve this. Since it could be solved with a while, what about a do while.

<pre lang="java">
int bar(Object o) {
  int counter = 2;
  do {
    counter--;
  } while(o == null && counter > 0);
  return counter;
}
</pre>

You could likely solve it with a for loop too, but that would require an f.

A case statement is the next obvious branching construct. However, you cannot switch on a condition such as does n == null. But you can switch on integers, and the identityHashCode is good choice because the identityHashCode for null is 0:

<pre lang="java">
int bar(Object o) {
  int hash = System.identityHashCode(o);
  switch(hash) {
    case 0: return 0;
  }
  return 1;
}
</pre>

Another conditional construct is the catch block, since it only enters into the catch block if an exception is thrown. It’s pretty easy to raise an exception with a null reference:

<pre lang="java">
int bar(Object o) {
  try {
    o.equals(o);
    return 1;
  } catch(NullPointerException e) {
    return 0;
  }
}
</pre>

Finally, I took the original question literally and wrote the if statement without an f. This can be done by replacing the character with its unicode equivalent. I think this was a little cheeky and not really in the spirit of the question though:

<pre lang="java">
int bar(Object o) {
  i\u0066(o == null ) 
    return 0;
  else 
    return 1;
  }
}
</pre>

Does anyone else have other suggestions for how to solve this? Or maybe Marc should just take Jesper’s advice:

14 Comments
  • Wolfgang Geck
    Posted at 9:28 am, August 6, 2016

    int bar(Object o) {
    return Boolean.compare(true, Objects.isNull(o));
    }

  • Howlger
    Posted at 1:27 pm, August 6, 2016

    int bar(Object o) {
    Set set = new HashSet();
    set.add(null);
    set.add(o);
    return set.size() – 1;
    }

    PS: Case statement: there is no guarantee that System.identityHashCode(o) returns also zero for a non-null object.

  • Tarjei
    Posted at 5:20 pm, August 6, 2016

    int bar(Object o) {
    return Optional.ofNullable(o).map(v -> 1).orElse(0);
    }

  • Tarjei
    Posted at 5:21 pm, August 6, 2016

    Bah, one f to many 🙂

  • Frank Appel
    Posted at 5:46 pm, August 6, 2016

    How about using streams with Java 8:

    int bar( Object o ) {
    return Arrays.asList( o )
    .stream()
    .filter( e -> e != null )
    .toArray().length;
    }

  • Jaroslav Tulach
    Posted at 8:18 am, August 7, 2016

    The

    int bar(Object o) {
    try {
    o.equals(o);
    return 1;
    } catch(NullPointerException e) {
    return 0;
    }
    }

    solution isn’t correct. Imagine one creates a class, overrides equals and always throws NullPointerException. The correct solution (used for example in Javac source code) is:

    int bar(Object o) {
    try {
    o.getClass();
    return 1;
    } catch(NullPointerException e) {
    return 0;
    }
    }

  • Ian Bull
    Posted at 11:00 pm, August 8, 2016

    @Jaroslav Great point!!!

  • shamaz
    Posted at 9:45 am, August 9, 2016

    return o == null ? 0 : 1;
    way more simple…

  • Alexander
    Posted at 3:18 pm, August 9, 2016

    int isNull(Object o) {
    return (o == null) ? return 0 : return 1;
    }

  • Neil
    Posted at 3:21 pm, August 9, 2016

    int bar(Object o) { return (o==NULL) ? 0 : 1; }

  • Neil
    Posted at 3:23 pm, August 9, 2016

    Sorry, didn’t see “?” was broken. I just highlighted it, did a copy/paste.

  • Chris
    Posted at 3:14 am, September 6, 2016

    Open http://www.amazon.com
    Search “Keyboard”
    Click Buy
    Wait…
    Plug in new keyboard

    Profit?

    Or use Alt-0102 to get an “f” character without the f key
    On screen keyboard?

    On Windows you’ve got “Windows-R”, run “charmap”

    There’s a million ways to skin this pig…