pagetop
Javablog
by Java coders, for Java codersRSS

Logical Puzzle

November 20th, 2007 by Sam

What do you think the output of this little Java program would be?

public static void main(String[] args) {
    if (stuff() | stuff())
        System.out.println("Goodbye");
}
 
static boolean stuff() {
    System.out.println("Hello");
    return true;
}

You may have expected it to print Hello and Goodbye because the true return value of stuff would force lazy evaluation of the conditional to return true.

However, if you ran the program you would see that Hello is printed twice, followed by Goodbye. The reason for this is because we used the bitwise OR ‘|’ not the logical OR ‘||’ and the bitwise OR always evaluates both sides of its parameters.

Inspired in part by Puzzle 42 of Java Puzzlers and also from a pretty horrible bug I found a while back.


This entry was posted by by Sam on Tuesday, November 20th, 2007 at 3:13 pm, and is filed under Java, Logic, Puzzle. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.



10 comments on “Logical Puzzle”

Not to be cheeky, but this is entry level stuff. I think I learnt this before I went to university in fact, the use of bitwise evaluators that is. It’s a very handy technique when you need to perform operations in a conditional statement without having to nest statements or have else conditions, or just having arbitrarily placed calls.

Immediately seeing that code though I knew what it would print - perhaps universities around the world don’t pick up on stuff like this? But now it’s mentioned, most of my coding books apart from some heavy C++ stuff mentions bitwise operations.

Sorry, I meant to say most of my coding books apart from heavy C++ stuff DO NOT mention bitwise operations, or if they do, it’s extremely brief and considered irrelevant.

It’s only a terrible technique if the programmer doesn’t know how to interpret it ;)

HelloGoodbye?

I’m not sure if Java do the same thing with a OR C language does.

@Kezzer… this is an example of the cause for a very subtle bug, not an introduction to bitwise operators. It illustrates that in Java it’s easy to mistype a bitwise operator for a logical one (and get unexpected results).

If you didn’t know to look for something odd in that code, would you have spotted it? Be honest now… it’s a single character difference to what you’d usually see, and because it’s valid syntax, no IDE is going to flag it up.

Personally, I think it is such bad coding style to use bitwise operators with boolean values that IDEs should flag it up! I can’t think of a single situation where non-lazy evaluation of logical conditions would possibly be a good thing.

I would have noticed it for the reason that it was my practise to use them originally. I wouldn’t use them now, and in fact, don’t use them now, but not for my sake, for the sake of others.

What I was originally getting at is that bitwise operations are a very basic construct learnt at entry-level programming. I had no intention of offending anyone and neither was I trying to demean anyone. Please, don’t take it so harshly :)

@Kezzer don’t worry, nobody’s offended… we’re just gob-smacked that you have used this as a technique in the past. ;-)

Sam, is it one of those lines of code you look at and literally cringe? I could say “I don’t know any better” then. You won’t find any of my current code, however, which uses this concept. Perhaps I’ve grown out of it, or just haven’t found a good use for it yet? ;)

I noticed the logical OR, but just because I was looking for something weird.

I have a suggestion. You have pretty good links here… I’d be glad if you could tell us about great blogs on Java and new techonologies. Thanks.

@KesheR you mean go through our blogroll in detail? (btw… it was a bitwise OR, not a logical OR)

Leave a comment

Markdown is supported.

To include code snippets in your comment, use

<pre><code># lang java
... code here ...
</code></pre>

or use 4 spaces at the start of the line instead of using code and pre tags.

Comment feed: RSS