Hi,
Could I ask to explain logic behind the statement:
int x2 = x1--;
First it will be the assignment (=) than decrement of x1 - (--) postfix operation.
But how this logic comply with the rule that postfix operation has higher precedence than assignment (=) operation.
About Question enthuware.ocajp.i.v8.2.1402 :
Moderator: admin
-
- Posts: 30
- Joined: Tue Mar 24, 2015 2:59 pm
- Contact:
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1402 :
There are two things that you need to know - value of the expression and value of the variable.
An expression can be composed on multiple variables. The value of the expression is the value of the whole of the right hand side of = operator. While the value of a variable is just the value of a variable. For example, if you have a = 1 and b=1,
a = b+1; //here b+1 is an expression and b is a variable. The value of the expression is 2, and the value of the variable b is 1
a = b; //here b is an expression and b is a variable as well. The value of the expression is 1, and the value of the variable b is also 1
In expressions involving Post increment/decrement the value of the expression and value of the variable are not same. In case of int x2 = x1--; the expression is x-- and its value is the value of x before x is decremented. The value of the variable x is x-1. This is how postfix operators are defined in JLS 15.14.2.
So to answer your question, the rule is still valid if you look at the value of the expression, which is computed first and then assigned to the left hand side variable.
HTH,
Paul.
An expression can be composed on multiple variables. The value of the expression is the value of the whole of the right hand side of = operator. While the value of a variable is just the value of a variable. For example, if you have a = 1 and b=1,
a = b+1; //here b+1 is an expression and b is a variable. The value of the expression is 2, and the value of the variable b is 1
a = b; //here b is an expression and b is a variable as well. The value of the expression is 1, and the value of the variable b is also 1
In expressions involving Post increment/decrement the value of the expression and value of the variable are not same. In case of int x2 = x1--; the expression is x-- and its value is the value of x before x is decremented. The value of the variable x is x-1. This is how postfix operators are defined in JLS 15.14.2.
So to answer your question, the rule is still valid if you look at the value of the expression, which is computed first and then assigned to the left hand side variable.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 30
- Joined: Tue Mar 24, 2015 2:59 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1402 :
Thank you. Now much more clear.
-
- Posts: 1
- Joined: Wed Sep 02, 2020 7:03 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1402 :
x3 is ++(x2) => x2 becomes -3 first and then its value i.e. -3 is assigned to x3 is a tricky part.
I assigned all correctly before but ignored ++x2 changes x2 as well
I assigned all correctly before but ignored ++x2 changes x2 as well
Who is online
Users browsing this forum: No registered users and 45 guests