How does the boolean assignment operator work in Java?

There is a code.

  public class IfElseTest {
    public static void main(String...args) {
      boolean b = false;
      if (b == false) {
        System.out.println(b=false);
      }

      if (b = false) {
        System.out.println("if statement");
      } else {
        System.out.println("else statement");
      }
    }
  }

The output of this program is:

  • False

  • Else statement

And it is not clear why the expression in the fifth line System.out.println(b=false);is interpreted by the compiler as false. Although it is intuitively clear that if b was a lie, then b assign false must be true.

Question: why does the compiler perceive b = false as false?

Author: s_klepcha, 2015-09-15

5 answers

Good code formatting helps you understand it better.:) So I will write the code in the following form

boolean b = false;

if (b == false)
    System.out.println(b=false);

if (b = false)
    System.out.println("if statement");
else
    System.out.println("else statement");

In the first if-clause

if (b == false)
    System.out.println(b=false);

Variable b compares with the literal false, and since this variable was initialized with the same value

boolean b = false;

Then obviously the result of evaluating the if-clause expression, i.e. the result of comparing, is true. Therefore, the console outputs

    System.out.println(b=false);

In the second if-else-clause

if (b = false)
    System.out.println("if statement");
else
    System.out.println("else statement");

In the if condition of the variable b присваивается value false

if (b = false)

The result of this expression, that is, the assignment, is the result of the assignment**. Since the result is false, the code block after the if clause will not be executed, and control will switch to the else clause

else
    System.out.println("else statement");

And, accordingly, the console will be issued

    System.out.println("else statement");

If we compare these two if-clauses

if (b == false)
if (b = false)

Then in the first In the if clause, the value of the expression is the result of the comparison. Since false (i.e. b) is equal to itself, the result of the comparison is true. In the second if clause, the value of the expression is the result of assigning the value false to variable b, that is, the value of the if clause expression is false.

You could rewrite the second if clause as follows

if (( b = false) == false )

And then, in fact, it would become equivalent to the sentences from program starts

boolean b = false;

if (b == false)

And you would get the same result as in the first if-clause, since the value of the expression was not the assigned value of the variable b, but the result of the comparison.

 4
Author: Vlad from Moscow, 2015-09-15 09:09:52

I can't say the smart words. But intuitively, System.out.println(b=false); is the equivalent of

b=false;
System.out.println(b);

That is, the assignment operation is processed first, and then the value of the variable is output. That is, the assignment itself is not a boolean operation.

You can check with other types for the sake of interest, in theory it should output 10:

int a=5;
System.out.println(a = 10);

PS is not directly relevant, but I would recommend correcting the formatting, otherwise I have a guess, judging by your the indentation means that you can generally also slightly misinterpret the operations that are performed if the first condition is met.

 5
Author: Aries, 2015-09-15 08:44:45

In the first case, you are comparing a variable with a constant, and for the compiler, it is not an indicator that they are of Boolean type - the result is given for the "== " operator.

And in the line:

   if (b = false) {}

The compiler uses a Boolean variable for branching, at the same time assigning it a value. This is equivalent to writing:

   b = false;
   if (b) {}
 1
Author: s_klepcha, 2015-09-15 08:42:14

Why does the compiler treat b = false as false?

Because first:

  • the variable b is assigned the value false;
  • then the methodtoString() is called for the same variable .

Invoking print or println outputs a single value after converting the value using the appropriate toString method

 1
Author: ermak0ff, 2015-09-15 08:49:51

Why does the compiler treat b = false as false?

The fact is that you do not use the comparison operation in the if operator, which is written as ==, but perform the assignment operation = (evaluate the expression) in the condition check if

That is, in this case, it is not checked whether it is equal to b false, and the expression is evaluated, in which b is assigned the value false , and it is natural that the compiler perceives it this way, because that as a result of the operation b = false, its result will be false

 1
Author: pavlofff, 2015-09-15 08:52:05