1

I wonder how the below code return false.

String line ="";
if (line.length() > 0 && !line.startsWith("/*") || !line.startsWith("--")) {    
  return false;
}

What I know is if we use &&, execution starts from left and it proceeds only if left result is true, else it do not proceed.

but in the above code its returning false. The value of line.length() is 0, but its validating second condition and evaluating to true because the other two conditions are true and returning false.

Help me in understanding this operator.

Thank you.

4 Answers 4

3

You have two operators. The AND operator is evaluated first and returns false. Then the OR operator is evaluated and returns true, since the second operand of the OR operator is true:

if (line.length() > 0 && !line.startsWith("/*") || !line.startsWith("--"))
          false       &&      not evaluated
                     false                      ||         true
                                               true    

If you want the second operand of the AND operator to include the OR operator, you should add parentheses:

if (line.length() > 0 && (!line.startsWith("/*") || !line.startsWith("--")))
        false                             not evaluated
                     false
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your time. What I am telling is if ****line.length()>0**** is failing why its going to validate other things. It should short circuit right? Its should not validate other conditions and it should fail the if test. Correct me if I am wrong.
@Abdul It does short circuit, which is why !line.startsWith("/*") is never evaluated, however, || !line.startsWith("--") is not part of the operands of the the AND operator, so after the AND operator is evaluated to false, the OR is evaluated - (false || !line.startsWith("--") - returning true.
Thanks, after adding parentheses part, I understood.
0

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

Look at The conditional operators section. It says it clearly: the second condition is only evaluated if needed. Since there are no parentheses giving your conditions any hierarchy, it evaluates the first one. length isn't bigger than 0, so it returns false and it doesn't evaluate the other 2.

Comments

0

If you add brackets to your code it will be clearer e.g.

if ( (line.length() > 0 && !line.startsWith("/*") ) || !line.startsWith("--"))

The first operator && is evaluated to false inside the brackets while the second operator || is evaluated to true.

2 Comments

I know that, I am confused about Java Short circuit operators.
@Abdul You are not short-circuiting here. There are two different operators working. The left operator will return false or true and it will compare that value to the right side of ||. If you want to short-circuit it you have to use brackets around the statement
0

First, line.length() > 0 && !line.startsWith("/*") is false. Then, the evaluation continue to false || !line.startsWith("--") which is true. That's why the codes inside your if statement block is executed.

From my inference, I think what you really need is :

if (line.length() > 0 && (!line.startsWith("/*") || !line.startsWith("--")))

That is in which the power of parentheses shows off!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.