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

前端 未结 14 751
误落风尘
误落风尘 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:39

    Basically the first statement x had it's value 1 So Java compares 1 == to new x variable which won't be the same

    In the second one you said x=y which means the value of x changed and so when you call it again it'll be the same value hence why it's true and x ==x

提交回复
热议问题