Why is x == (x = y) not the same as (x = y) == x?

前端 未结 14 768
误落风尘
误落风尘 2021-01-29 21:11

Consider the following example:

class Quirky {
    public static void main(String[] args) {
        int x = 1;
        int y = 3;

        System.out.println(x =         


        
14条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-29 21:44

    In the first test you're checking does 1 == 3.

    In the second test your checking does 3 == 3.

    (x = y) assigns the value and that value is tested. In the former example x = 1 first then x is assigned 3. Does 1 == 3?

    In the latter, x is assigned 3, and obviously it's still 3. Does 3 == 3?

提交回复
热议问题