Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confused with return value of assignment operation in java

Tags:

java

I tried to understand how exactly the return value of assignment operation works. followed by this post "Java returns the assigned value".

    boolean b1 = false, b2 = false;
    if (b2 = b1 == false) {
        System.out.println("true");
    } else {
        System.out.println("false");
    }

b2 is true because of (b1 == false) return true, then the return of b2 assignment b2 = true

Or is it because of some other reason?

like image 267
Pauwelyn Avatar asked Oct 28 '25 05:10

Pauwelyn


1 Answers

You've got it right. The operator precedence rules make sure that first the == operator is evaluated. That's b1==false, yielding true. After that, the assigned is executed, setting b2 to true. Finally, the assignment operator returns the value as b2, which is evaluated by the if statement.

like image 177
Stephan Rauh Avatar answered Oct 29 '25 19:10

Stephan Rauh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!