user1580609
user1580609

Reputation: 53

Java Boolean expression behavior

This is my first post here so please forgive any protocol errors.

My question is simply trying to understand what is happening with the following Java code. I fully understand that the use of parentheses would clarify everything, but the resulting output seems to fly in the face of convention regarding Java order of operations.

public class Tester
{
   public static void main(String[] args)
   {
    int total=9, num=13;
    if (total>4 || ++num>15 && total>0)
    {
           System.out.println("short");
    }
    System.out.println(num);
   }
}

The output is: short 13

It is obvious the ++num did not execute. If strict order of operations had been observed it should have been the first thing to happen. It didn't. So next is the &&. If the && is done by order of precedence over the ||, then the same...the ++num should happen first. It didn't. So, to me it seems the output was determined with the || executed first, shortciruiting the ++num and then, working with the &&, resulted in the short being printed. Were the order of operation rules simply ignored and the Boolean expression executed left to right? Is the increment operator causing the irregular behavior?

Thanks for any insight as to what is actually going on with this code.

Upvotes: 5

Views: 1379

Answers (5)

Jamie Nhu
Jamie Nhu

Reputation: 41

i think it doesn't change the actual value of the variable when using in the if clause . The value stored in the memory remains the same. it is like num+1>15 in the expression

Upvotes: 0

obataku
obataku

Reputation: 29646

This has nothing to do with precedence/associativity (which deals with how expressions are parsed)... this has to do with Java evaluation order -- which is left to right. You appear to be misunderstanding how short-circuit logic works.

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.


Now, let's attempt to evaluate this expression incrementally...

total > 4 || ++num > 15 && total > 0

Since total > 4 evaluates to true, the condition evaluates to true and the if branch is taken immediately rather than evaluating the rest of the conditional.

If you change total to equal 4, then the left-operand (total > 4) of the short-circuit OR is false and thus it evaluates the right-operand (++num > 15 && total > 0).

If you change num to equal 15, then the short-circuit AND left-operand (++num > 15) evaluates to true and thus it finally evaluates the AND right-operand (total > 0) to determine whether the conditional is true. If total > 0 is false, then the conditional is also false.

Below is the code rewritten for clarity to highlight the flow.

if (total > 4) {
  System.out.println("short");
} else {
  if (++num > 15) {
    if (total > 0) {
      System.out.println("short");
    }
  }
}
System.out.println(num);

You can read more on Java conditional operators in the relevant Java Tutorial.

Upvotes: 6

David Titarenco
David Titarenco

Reputation: 33386

What's going on is short-circuit evaluation. total > 4 is true, making the entire conditional true. We don't even need to look at what follows after ||. This is a very common run-time optimization also present in C, C++, and Javascript (among many many others). You observed its' side-effect.

Note that short-circuiting can be used to your advantage (checking left-side of the || or && for a (non-)null value, for example) or it can confuzzle you (like it did in your question). Use it wisely, young padawan.

Upvotes: 3

Jamie Nhu
Jamie Nhu

Reputation: 41

the computer will under you boolean expression like this total>4 || (++num>15 && total>0) . It is like the expression in standard SQL. It finds the && and grabs 2 expressions on the left and right then put them in a bracket like i just did. In your code, total=9>4 and ++num=14<15 and total=9>0 so the result is true ||(false && true) = true. The terminal prints out "short" and num which is 13.

Hope my answer will help you. If i am wrong, please correct me

Upvotes: 0

Alex Coleman
Alex Coleman

Reputation: 7326

By using the double or (||), you activate java's short circuiting; (use a single | if you don't want this). As soon as it gets to a point where everything is true, and it hits a short circuit or (||), (or somethings false, and it hits a short circuit & (&&)), it will break out. So In your case, total is bigger than 4, so it stops there (due to the ||), and returns true to the if statement, and prints out short; it never reaches the part that increments the num variable, as it sees no need to check that

Upvotes: 4

Related Questions