Jump to content

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


Recommended Posts

Posted (edited)

Consider the example:

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

        System.out.println(x == (x = y)); // false
        x = 1; // reset
        System.out.println((x = y) == x); // true
     }
}


I am unsure whether the Java Language Specification contains a rule regarding what value should be loaded to compare with the right side (x = y) of an equation, given that the value on the right side should be calculated first according to the order implied by brackets.

Why does the first expression evaluate to false, but the second evaluate to true? I would have expected (x = y) to be evaluated first, and then it would compare x with itself (3) and return true

int oldX = x;
x = y;
return oldX == y;

which, being even simpler than x86 CMPXCHG instruction, deserved a shorter expression in Java.

 

Spoiler

 

 

Edited by Phi for All
No advertising, please.
Posted
2 hours ago, Bunty12 said:

Consider the example:

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

        System.out.println(x == (x = y)); // false
        x = 1; // reset
        System.out.println((x = y) == x); // true
     }
}


I am unsure whether the Java Language Specification contains a rule regarding what value should be loaded to compare with the right side (x = y) of an equation, given that the value on the right side should be calculated first according to the order implied by brackets.

Why does the first expression evaluate to false, but the second evaluate to true? I would have expected (x = y) to be evaluated first, and then it would compare x with itself (3) and return true

int oldX = x;
x = y;
return oldX == y;

which, being even simpler than x86 CMPXCHG instruction, deserved a shorter expression in Java.

Question reference: https://www.interviewbit.com/java-interview-questions/

  Reveal hidden contents

 

 

I understand that in evaluating == the LS gets evaluated first, then the RS, then they get compared. Let's say in the beginning x is 1 and y is 2.

So, in x == (x=y):

1) the LS evaluates to 1

2) x=y gets executed; x is now 2

3) the RS evaluates to 2

4) 1 == 2 returns false.

In (x=y) == x:

1) the LS gets executed; x is 2

2) the LS evaluates to 2

3) the RS evaluates to 2

4) 2 == 2 returns true

Posted (edited)

In C# it is the same:

using System;
class Test {
	static void Main( string [] args ) {
    	int x = 1;
        int y = 3;
        Console.WriteLine("pre x={0} y={1}", x, y );
        Console.WriteLine(x == (x = y)); // false
        Console.WriteLine("post x={0} y={1}", x, y );
        x = 1; // reset
        Console.WriteLine("pre x={0} y={1}", x, y );
        Console.WriteLine((x = y) == x); // true
        Console.WriteLine("post x={0} y={1}", x, y );
	}
}

 

Quote

pre x=1 y=3
False
post x=3 y=3
pre x=1 y=3
True
post x=3 y=3

 

In languages that allow overloading of operators, you can try to create your own class with overloaded assignment and comparison operators and do logging there.

Edited by Sensei

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.