Interpretation of logic operators in Java regarding short-circuit

Asked

Viewed 278 times

4

I read that question Doubt about logical operators && e || in Java, but I follow with the doubt. Solving some exercises on Java I find such a statement:

Operators & and | operate in the same way as operators && and ||, but always evaluate both operands.

The answer says that the affirmative is CORRECT, but my question is:

  1. In the case of the above quote, we will always evaluate both operands?
  2. Operators || and && do not generate short-circuit? And with that they don’t work the same way or am I making some mistake in interpretation?
  • Note that what you linked was a shell script response that briefly mentions the short circuit. The understanding of that answer should be taken with some grains of salt to extrapolate to Java

  • I realized that! But it was the most coherent search I found searching for the word 'short circuit', but the links that Maniero posted made the short circuit clearer!

1 answer

10


In the case of the above quote, we will always evaluate both operands?

No, the most visible difference is just this. Using && and || there is the so-called short circuit, or if the expression of the left proves sufficient (verdeiro when using ||; or false when using &&) the right expression is not even executed. Already with & and | the two expressions are executed, even if you don’t need to give the desired result.

Operators || and && do not generate short-circuit?

Exactly the opposite they generate, and this link has nothing to do with short circuit. This has. And this also.

And with that they don’t work the same way or am I making some mistake in interpretation?

They do not work identically, the text you read is misspelled. It states something to deny next. I don’t know where you found it, but choose your sources well, some people are not committed to the right thing. The other answer is also wrong because it states that the two work equally in the text and in the examples it reverses what it is saying and gives wrong results.

Actually this is not even the only difference. Operators & and | neither are logical are bit handlers, only if the value is boolean the result turns out to be the same because it only has 1 bit that matters.

So you can see better what you do and the result you give:

class Main {
    public static void main(String[] args) {
        if (teste("1", false) & teste("2", true)) System.out.println("false & true entrou");
        if (teste("3", false) && teste("4", true)) System.out.println("false && true entrou");
        if (teste("5", true) & teste("6", false)) System.out.println("true & false entrou");
        if (teste("7", true) && teste("8", false)) System.out.println("true && false entrou");
        if (teste("9", false) | teste("10", true)) System.out.println("false | true entrou");
        if (teste("11", false) || teste("12", true)) System.out.println("false || true entrou");
        if (teste("13", true) | teste("14", false)) System.out.println("true | false entrou");
        if (teste("15", true) || teste("16", false)) System.out.println("true || false entrou");
    }
    public static boolean teste(String mensagem, boolean valor) {
        System.out.println(mensagem);
        return valor;
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.