Why x == (x = y) is not the same as (x = y) == x?
up vote
13
down vote
favorite
Consider the following 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'm not sure if there is an item in Java Language Specification that dictates loading previous value of a variable for comparison with the right side (x = y
) which, by the order implied by brackets, should be calculated first.
Why does the first expression evaluate to false
, but the second evaluates to true
? I would have expected (x = y)
to be evaluated first, and then it would compare x
with itself (3
) and return true
.
java variable-assignment operator-precedence jls
|
show 1 more comment
up vote
13
down vote
favorite
Consider the following 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'm not sure if there is an item in Java Language Specification that dictates loading previous value of a variable for comparison with the right side (x = y
) which, by the order implied by brackets, should be calculated first.
Why does the first expression evaluate to false
, but the second evaluates to true
? I would have expected (x = y)
to be evaluated first, and then it would compare x
with itself (3
) and return true
.
java variable-assignment operator-precedence jls
9
The left hand side is always evaluated before the right hand side. The brackets don't make a difference to that.
– Louis Wasserman
6 hours ago
@LouisWasserman The term evaluate is, in my opinion, inapplicable here becausex
does not need to be evaluated, it just loaded from memory.
– John McClane
6 hours ago
3
Evaluating the expressionx = y
is certainly relevant, and causes the side effect thatx
is set to the value ofy
.
– Louis Wasserman
6 hours ago
@LouisWasserman Sure. I knew that, and it was the main idea that lead me toquickReplaceAndCompare()
.
– John McClane
6 hours ago
5
Do yourself and your teammates a favor and don't mix state mutation into the same line as state examination. Doing so drastically reduces the readability of your code. (There are some cases where it's absolutely necessary because of atomicity requirements, but functions for those already exist and their purpose would be instantly recognized.)
– jpmc26
3 hours ago
|
show 1 more comment
up vote
13
down vote
favorite
up vote
13
down vote
favorite
Consider the following 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'm not sure if there is an item in Java Language Specification that dictates loading previous value of a variable for comparison with the right side (x = y
) which, by the order implied by brackets, should be calculated first.
Why does the first expression evaluate to false
, but the second evaluates to true
? I would have expected (x = y)
to be evaluated first, and then it would compare x
with itself (3
) and return true
.
java variable-assignment operator-precedence jls
Consider the following 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'm not sure if there is an item in Java Language Specification that dictates loading previous value of a variable for comparison with the right side (x = y
) which, by the order implied by brackets, should be calculated first.
Why does the first expression evaluate to false
, but the second evaluates to true
? I would have expected (x = y)
to be evaluated first, and then it would compare x
with itself (3
) and return true
.
java variable-assignment operator-precedence jls
java variable-assignment operator-precedence jls
edited 2 hours ago
pushkin
3,859102550
3,859102550
asked 6 hours ago
John McClane
437113
437113
9
The left hand side is always evaluated before the right hand side. The brackets don't make a difference to that.
– Louis Wasserman
6 hours ago
@LouisWasserman The term evaluate is, in my opinion, inapplicable here becausex
does not need to be evaluated, it just loaded from memory.
– John McClane
6 hours ago
3
Evaluating the expressionx = y
is certainly relevant, and causes the side effect thatx
is set to the value ofy
.
– Louis Wasserman
6 hours ago
@LouisWasserman Sure. I knew that, and it was the main idea that lead me toquickReplaceAndCompare()
.
– John McClane
6 hours ago
5
Do yourself and your teammates a favor and don't mix state mutation into the same line as state examination. Doing so drastically reduces the readability of your code. (There are some cases where it's absolutely necessary because of atomicity requirements, but functions for those already exist and their purpose would be instantly recognized.)
– jpmc26
3 hours ago
|
show 1 more comment
9
The left hand side is always evaluated before the right hand side. The brackets don't make a difference to that.
– Louis Wasserman
6 hours ago
@LouisWasserman The term evaluate is, in my opinion, inapplicable here becausex
does not need to be evaluated, it just loaded from memory.
– John McClane
6 hours ago
3
Evaluating the expressionx = y
is certainly relevant, and causes the side effect thatx
is set to the value ofy
.
– Louis Wasserman
6 hours ago
@LouisWasserman Sure. I knew that, and it was the main idea that lead me toquickReplaceAndCompare()
.
– John McClane
6 hours ago
5
Do yourself and your teammates a favor and don't mix state mutation into the same line as state examination. Doing so drastically reduces the readability of your code. (There are some cases where it's absolutely necessary because of atomicity requirements, but functions for those already exist and their purpose would be instantly recognized.)
– jpmc26
3 hours ago
9
9
The left hand side is always evaluated before the right hand side. The brackets don't make a difference to that.
– Louis Wasserman
6 hours ago
The left hand side is always evaluated before the right hand side. The brackets don't make a difference to that.
– Louis Wasserman
6 hours ago
@LouisWasserman The term evaluate is, in my opinion, inapplicable here because
x
does not need to be evaluated, it just loaded from memory.– John McClane
6 hours ago
@LouisWasserman The term evaluate is, in my opinion, inapplicable here because
x
does not need to be evaluated, it just loaded from memory.– John McClane
6 hours ago
3
3
Evaluating the expression
x = y
is certainly relevant, and causes the side effect that x
is set to the value of y
.– Louis Wasserman
6 hours ago
Evaluating the expression
x = y
is certainly relevant, and causes the side effect that x
is set to the value of y
.– Louis Wasserman
6 hours ago
@LouisWasserman Sure. I knew that, and it was the main idea that lead me to
quickReplaceAndCompare()
.– John McClane
6 hours ago
@LouisWasserman Sure. I knew that, and it was the main idea that lead me to
quickReplaceAndCompare()
.– John McClane
6 hours ago
5
5
Do yourself and your teammates a favor and don't mix state mutation into the same line as state examination. Doing so drastically reduces the readability of your code. (There are some cases where it's absolutely necessary because of atomicity requirements, but functions for those already exist and their purpose would be instantly recognized.)
– jpmc26
3 hours ago
Do yourself and your teammates a favor and don't mix state mutation into the same line as state examination. Doing so drastically reduces the readability of your code. (There are some cases where it's absolutely necessary because of atomicity requirements, but functions for those already exist and their purpose would be instantly recognized.)
– jpmc26
3 hours ago
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
20
down vote
==
is a binary equality operator.
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
Java 11 Specification > Evaluation Order > Evaluate Left-Hand Operand First
1
@JohnMcClane "by the order implied by brackets" - the parentheses don't set any order, they form an operand for==
. Without the parentheses, neither of these expressions would be syntactically correct.
– Andrew Tobilko
4 hours ago
add a comment |
up vote
12
down vote
As LouisWasserman sad, the expression is evaluated left to right. And java doesn't care what "evaluate" actually does, it only cares about generating a (non volatile, final) value to work with.
//the example values
x = 1;
y = 2;
So in quickReplaceAndCompare
the following is done:
x == (x = y)
1 == (x = y)
1 == (x = 2) //assign 2 to x, returns 2
1 == 2
false
and in badReplaceAndCompare()
:
(x = y) == x
(x = 2) == x //assign 2 to x, returns 2
2 == x
2 == 2
true
note that badReplaceAndCompare()
will always return true, because you are effectively comparing the assignment of a value to the variable it is assigned to, and a = b
and b
will, evaluated in that order, always be the same by definition.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
20
down vote
==
is a binary equality operator.
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
Java 11 Specification > Evaluation Order > Evaluate Left-Hand Operand First
1
@JohnMcClane "by the order implied by brackets" - the parentheses don't set any order, they form an operand for==
. Without the parentheses, neither of these expressions would be syntactically correct.
– Andrew Tobilko
4 hours ago
add a comment |
up vote
20
down vote
==
is a binary equality operator.
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
Java 11 Specification > Evaluation Order > Evaluate Left-Hand Operand First
1
@JohnMcClane "by the order implied by brackets" - the parentheses don't set any order, they form an operand for==
. Without the parentheses, neither of these expressions would be syntactically correct.
– Andrew Tobilko
4 hours ago
add a comment |
up vote
20
down vote
up vote
20
down vote
==
is a binary equality operator.
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
Java 11 Specification > Evaluation Order > Evaluate Left-Hand Operand First
==
is a binary equality operator.
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
Java 11 Specification > Evaluation Order > Evaluate Left-Hand Operand First
edited 5 hours ago
answered 5 hours ago
Andrew Tobilko
24.3k84082
24.3k84082
1
@JohnMcClane "by the order implied by brackets" - the parentheses don't set any order, they form an operand for==
. Without the parentheses, neither of these expressions would be syntactically correct.
– Andrew Tobilko
4 hours ago
add a comment |
1
@JohnMcClane "by the order implied by brackets" - the parentheses don't set any order, they form an operand for==
. Without the parentheses, neither of these expressions would be syntactically correct.
– Andrew Tobilko
4 hours ago
1
1
@JohnMcClane "by the order implied by brackets" - the parentheses don't set any order, they form an operand for
==
. Without the parentheses, neither of these expressions would be syntactically correct.– Andrew Tobilko
4 hours ago
@JohnMcClane "by the order implied by brackets" - the parentheses don't set any order, they form an operand for
==
. Without the parentheses, neither of these expressions would be syntactically correct.– Andrew Tobilko
4 hours ago
add a comment |
up vote
12
down vote
As LouisWasserman sad, the expression is evaluated left to right. And java doesn't care what "evaluate" actually does, it only cares about generating a (non volatile, final) value to work with.
//the example values
x = 1;
y = 2;
So in quickReplaceAndCompare
the following is done:
x == (x = y)
1 == (x = y)
1 == (x = 2) //assign 2 to x, returns 2
1 == 2
false
and in badReplaceAndCompare()
:
(x = y) == x
(x = 2) == x //assign 2 to x, returns 2
2 == x
2 == 2
true
note that badReplaceAndCompare()
will always return true, because you are effectively comparing the assignment of a value to the variable it is assigned to, and a = b
and b
will, evaluated in that order, always be the same by definition.
add a comment |
up vote
12
down vote
As LouisWasserman sad, the expression is evaluated left to right. And java doesn't care what "evaluate" actually does, it only cares about generating a (non volatile, final) value to work with.
//the example values
x = 1;
y = 2;
So in quickReplaceAndCompare
the following is done:
x == (x = y)
1 == (x = y)
1 == (x = 2) //assign 2 to x, returns 2
1 == 2
false
and in badReplaceAndCompare()
:
(x = y) == x
(x = 2) == x //assign 2 to x, returns 2
2 == x
2 == 2
true
note that badReplaceAndCompare()
will always return true, because you are effectively comparing the assignment of a value to the variable it is assigned to, and a = b
and b
will, evaluated in that order, always be the same by definition.
add a comment |
up vote
12
down vote
up vote
12
down vote
As LouisWasserman sad, the expression is evaluated left to right. And java doesn't care what "evaluate" actually does, it only cares about generating a (non volatile, final) value to work with.
//the example values
x = 1;
y = 2;
So in quickReplaceAndCompare
the following is done:
x == (x = y)
1 == (x = y)
1 == (x = 2) //assign 2 to x, returns 2
1 == 2
false
and in badReplaceAndCompare()
:
(x = y) == x
(x = 2) == x //assign 2 to x, returns 2
2 == x
2 == 2
true
note that badReplaceAndCompare()
will always return true, because you are effectively comparing the assignment of a value to the variable it is assigned to, and a = b
and b
will, evaluated in that order, always be the same by definition.
As LouisWasserman sad, the expression is evaluated left to right. And java doesn't care what "evaluate" actually does, it only cares about generating a (non volatile, final) value to work with.
//the example values
x = 1;
y = 2;
So in quickReplaceAndCompare
the following is done:
x == (x = y)
1 == (x = y)
1 == (x = 2) //assign 2 to x, returns 2
1 == 2
false
and in badReplaceAndCompare()
:
(x = y) == x
(x = 2) == x //assign 2 to x, returns 2
2 == x
2 == 2
true
note that badReplaceAndCompare()
will always return true, because you are effectively comparing the assignment of a value to the variable it is assigned to, and a = b
and b
will, evaluated in that order, always be the same by definition.
answered 5 hours ago
Poohl
30319
30319
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53749841%2fwhy-x-x-y-is-not-the-same-as-x-y-x%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
9
The left hand side is always evaluated before the right hand side. The brackets don't make a difference to that.
– Louis Wasserman
6 hours ago
@LouisWasserman The term evaluate is, in my opinion, inapplicable here because
x
does not need to be evaluated, it just loaded from memory.– John McClane
6 hours ago
3
Evaluating the expression
x = y
is certainly relevant, and causes the side effect thatx
is set to the value ofy
.– Louis Wasserman
6 hours ago
@LouisWasserman Sure. I knew that, and it was the main idea that lead me to
quickReplaceAndCompare()
.– John McClane
6 hours ago
5
Do yourself and your teammates a favor and don't mix state mutation into the same line as state examination. Doing so drastically reduces the readability of your code. (There are some cases where it's absolutely necessary because of atomicity requirements, but functions for those already exist and their purpose would be instantly recognized.)
– jpmc26
3 hours ago