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:

int bar(Object o) {
  while(o == null) {
    return 0;
  }
  return 1;
}

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.

int bar(Object o) {
  int counter = 2;
  do {
    counter--;
  } while(o == null && counter > 0);
  return counter;
}

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:

int bar(Object o) {
  int hash = System.identityHashCode(o);
  switch(hash) {
    case 0: return 0;
  }
  return 1;
}

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:

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

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:

int bar(Object o) {
  i\u0066(o == null ) 
    return 0;
  else 
    return 1;
  }
}

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

14 Responses to “Branching Without an if, A Java Puzzle”

  1. Wolfgang Geck says:

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

  2. Howlger says:

    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.

  3. Ian Bull says:

    @Holger,
    This is a pretty unique solution. Cool! As for the identityHashCode returning 0 for non-null objects, that’s a good point. Although this is often the initial memory location of the object, so in practice it’s unlikely to be zero. But point taken, if my keyboard breaks and I don’t have an ‘f’ key, I won’t use this solution ;-).

  4. Tarjei says:

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

  5. Tarjei says:

    Bah, one f to many :)

  6. Frank Appel says:

    How about using streams with Java 8:

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

  7. Jaroslav Tulach says:

    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;
    }
    }

  8. Ian Bull says:

    @Jaroslav Great point!!!

  9. shamaz says:

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

  10. Alexander says:

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

  11. Neil says:

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

  12. Neil says:

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

  13. Ian Bull says:

    Yeah, it was a little too easy if you allow the ‘?’. Creativity was the name of the game here : )

  14. Chris says:

    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…

Leave a Reply

14 responses so far

Written by . Published in Categories: Editors choice, Planet Eclipse

Looking for a job?

X
Karlsruhe / Remote
JavaScript
Mobile
Karlsruhe / Victoria / Remote
Windows
Mobile
Karlsruhe / Victoria / Remote
Java
Android
Mobile