8

Why does this if statement, with an assignment and equality check, evaluate to false?

public static void test() {

    boolean test1 = true; 
    if (test1 = false || test1 == false) {
        System.out.println("Yes");
    } else {
        System.out.println("No");
    }       
}

Why is this printing No?

Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
dhaval joshi
  • 490
  • 1
  • 3
  • 15
  • that is why you should always use parenthesis in these kind of situations – Giorgi Moniava Oct 11 '16 at 08:17
  • 1
    The answer you got are not explaining the problem fully or are dead wrong. Please refer to the linked question, which is _exactly_ the same. – Tunaki Oct 11 '16 at 08:18

4 Answers4

14

Because of operator precedence. It is equivalent to this:

boolean test1 = true; 
if (test1 = (false || test1 == false)) {
...
}

The part in brackets evaluates to false.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Axel
  • 13,939
  • 5
  • 50
  • 79
  • @Reign: `test1 = false` both sets the value *and* has a result, `false`. The result of assignment is the value assigned. – T.J. Crowder Oct 11 '16 at 07:29
-1

you are assigning value to var in if statement, but not using it. If you want to assign in if statement and use, you can do this:

boolean test1;
if ((test1 = false) == true) {
    System.out.println("Yes");
} else {
    System.out.println("No");
}
D. Pineda
  • 66
  • 2
-1

You have the following operators in your if statement, which are priorized like this: equality > logical OR > assignment

The if is then reducing like this:

(test1 = false || test1 == false) //substituing test1 with its value

(test1 = false || true == false) //evaluating equality

(test1 = false || false) //evaluating logical OR

(test1 = false) //evaluating assignment

(test1) //substituing test1 with its value

(false) //evaluating primitive value, going to else block

Hence printing No.

Spotted
  • 4,021
  • 17
  • 33
  • No. [Java evaluates left-to-right](https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7). The assignment is done before equality. – Andy Turner Oct 11 '16 at 07:35
  • @AndyTurner If the assignment is evaluated first, it should have printed *Yes*, right ? – Spotted Oct 11 '16 at 07:36
  • @AndyTurner `((test1 = false) || test1 == false)` would have printed *Yes*. – Spotted Oct 11 '16 at 07:37
  • `((test1 = false) || test1 == false)` is a different expression. – Andy Turner Oct 11 '16 at 07:38
  • @AndyTurner I know, but it was to emphasize that without parenthesis the assignment can't be evaluated first, otherwise the OP's code would have printed *Yes*. – Spotted Oct 11 '16 at 07:39
  • What I meant to say was *equality is not evaluated first*. The first operand of the `||` is evaluated first, is found to be false, and then the equality check is performed. – Andy Turner Oct 11 '16 at 07:40
  • @AndyTurner If I follow your reasoning it should be: `(test1 = false || test1 == false)` => `(false || test1 == false)` => `(false || false == false)` => `(false || true)` => `(true)` ? – Spotted Oct 11 '16 at 07:43
  • No. It should be `(test1 = false || test1 == false) => (test1 = test1 == false) => (test1 = false) => (false)`, because that's how the operator precedence works. – Andy Turner Oct 11 '16 at 07:45
  • @AndyTurner I don't get it. In your second step, test1's value should be `false` because the assignment has been performed ? – Spotted Oct 11 '16 at 07:50
-2

The result of this code will print "no" as you are assigning the test1 as false test1 = false the if statement then evaluates to false because you then you evaluate the test1 == false and else block executes

this might be what you are looking for :

   if ((test1 == false) || (test1 == false)) //this line if the first answer is true it returns true without checking the seconded statement.  
     // if (test1 == false) would be the, same elauates as true 
     {
            System.out.println("Yes");
        } else {
        System.out.println("No");
    }       

look in the shortcircuit or operator this might help you better grasp the issue

Reign
  • 289
  • 1
  • 9