0
   import java.util.*;
   class Example{
    public static void main(String args[]){
        int x =10;
        boolean b = true;
        if((b=false)==false){
            
            System.out.println(b);
        }
    }
}

output : false how this == and = operators work in java when both used together in this if clause with a Boolean expression?

  • 4
    It assigns `false` to `b` and then does the comparison. So the statement will always evaluate to `true` since `false == false` is always `true`. – WJS Jun 03 '21 at 14:52
  • Also, compare that to `(b = (b == false))` which also assigns `false` to `b`. But the assignment is the result of the comparison and that determines the result of the condition which results in `false` and `b` is not printed. If `b` were initially set to `false`, then the statement would evaluate to `true`, `b` would be assigned `true`, and `true` would be printed. – WJS Jun 03 '21 at 15:04
  • b=false will assign the boolean value false to variable b. In the println statement, you are only printing the value in variable b, which is false. However, your expression in that if stateent will evaluate to true since you are comparing false to false, like false == false -> true. But then false will be printed out since that's what you assigned into the variable b. – Sydney Molobela Jun 03 '21 at 15:26

0 Answers0