About Question enthuware.ocajp.i.v7.2.1040 :
Moderator: admin
Re: About Question enthuware.ocajp.i.v7.2.1040 :
Could you explain/postLink how are we evaluating these expressions because i thought that "a" becomes 4 and then we add it to itself
so 4 + 4 , but my guess is wrong.
a += (a = 4);
b = b + (b = 5);
so 4 + 4 , but my guess is wrong.
a += (a = 4);
b = b + (b = 5);
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1040 :
a += (a=4) =>ETS User wrote:Could you explain/postLink how are we evaluating these expressions because i thought that "a" becomes 4 and then we add it to itself
so 4 + 4 , but my guess is wrong.
a += (a = 4);
a = a + (a=4) =>
a = 10 + (value of expression a=4)
The value of a is 10 before a=4 is executed so you put 10 for the first a.
Remember that every assignment expression itself has a value so (a = 4) itself has a value, which is the value that you are assigning to the left hand side i.e. 4.
At this point the value of a is 4, but there one more operation left.
a = 10 + 4
a = 14. So a is set to 14.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1040 :
I thoroughly understand the point.
However, int a = 10; a = a + (a = 4); By having the parenthesis should not it be prioritized the parenthesis bit of the expression rather than the left part to the "+"?
In few words, why a gets the value of 10 if the mini expression within parenthesis should be valuated first?
Taking into account that () are > than + in terms of priority.
Thanks in advance.
However, int a = 10; a = a + (a = 4); By having the parenthesis should not it be prioritized the parenthesis bit of the expression rather than the left part to the "+"?
In few words, why a gets the value of 10 if the mini expression within parenthesis should be valuated first?
Taking into account that () are > than + in terms of priority.
Thanks in advance.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1040 :
a doesn't get 10 in your example. a gets 14.The_Nick wrote:I thoroughly understand the point.
However, int a = 10; a = a + (a = 4); By having the parenthesis should not it be prioritized the parenthesis bit of the expression rather than the left part to the "+"?
In few words, why a gets the value of 10 if the mini expression within parenthesis should be valuated first?
Taking into account that () are > than + in terms of priority.
Thanks in advance.
YOu can think of it like this: first put the values of the variables and then apply priorty to evaluate. So:
a = 10 + ( a = 4)
a = 10 + 4
a = 14
Re: About Question enthuware.ocajp.i.v7.2.1040 :
I did not mean that the result was 10, sorry if I did not make it quite clear. it was just reporting the code.admin wrote:a doesn't get 10 in your example. a gets 14.The_Nick wrote:I thoroughly understand the point.
However, int a = 10; a = a + (a = 4); By having the parenthesis should not it be prioritized the parenthesis bit of the expression rather than the left part to the "+"?
In few words, why a gets the value of 10 if the mini expression within parenthesis should be valuated first?
Taking into account that () are > than + in terms of priority.
Thanks in advance.
YOu can think of it like this: first put the values of the variables and then apply priorty to evaluate. So:
a = 10 + ( a = 4)
a = 10 + 4
a = 14
For what I understood every left variable gets saved for the right hand expression. isnt' it?
let's say we have:
int a= 1;
a+= (a =4)+(a=4+a);
the result will be 13, basically every variable on the left hand side of a binary operator gets temporary saved and used for the right hand side operand.
The_Nick
Re: About Question enthuware.ocajp.i.v7.2.1040 :
This is kind of interesting:
Prints: z=19
Prints: z=16
So, the last z is not a 7, but a 4, if it is within the same parens as the assignment to 7. So, I take it that the assignment will not affect a variable in the same immediate parens.
Of course, anyone who actually programs like this is truly asking for trouble.
Code: Select all
int z = 1;
z += (z=4) + (z=7) + z;
System.out.println("z=" + z);
Code: Select all
int z = 1;
z += (z=4) + (z=7 + z);
System.out.println("z=" + z);
So, the last z is not a 7, but a 4, if it is within the same parens as the assignment to 7. So, I take it that the assignment will not affect a variable in the same immediate parens.
Of course, anyone who actually programs like this is truly asking for trouble.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1040 :
From your answer:
Why isn't 2nd 'a' from left set to 4 instead of 10 ?
From the expression a = a + (a=4)a += (a =4) is same as a = a + (a=4). First, a's value of 10 is kept aside and (a=4) is evaluated. The statement (a=4) assigns 4 to a and the whole statement returns the value 4. Thus, 10 and 4 are added and assigned back to a.
Why isn't 2nd 'a' from left set to 4 instead of 10 ?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1040 :
That is because an expression is evaluated from left to right.
Expression evaluation requires three things to be taken into account - precedence, associativity, and evaluation order. It will a bit too much to explain it in a forum post, so you should google it.
Section 5.1.6 of OCAJP 8 Fundamentals by Hanumant Deshmukh also explains it. You can get it from here.
Expression evaluation requires three things to be taken into account - precedence, associativity, and evaluation order. It will a bit too much to explain it in a forum post, so you should google it.
Section 5.1.6 of OCAJP 8 Fundamentals by Hanumant Deshmukh also explains it. You can get it from here.
Who is online
Users browsing this forum: No registered users and 3 guests