About Question enthuware.ocajp.i.v7.2.1040 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

Re: About Question enthuware.ocajp.i.v7.2.1040 :

Post by ETS User »

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);

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1040 :

Post by admin »

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=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.
If you like our products and services, please help us by posting your review here.

The_Nick

Re: About Question enthuware.ocajp.i.v7.2.1040 :

Post by The_Nick »

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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1040 :

Post by admin »

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.
a doesn't get 10 in your example. a gets 14.
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
If you like our products and services, please help us by posting your review here.

The_Nick

Re: About Question enthuware.ocajp.i.v7.2.1040 :

Post by The_Nick »

admin wrote:
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.
a doesn't get 10 in your example. a gets 14.
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
I did not mean that the result was 10, sorry if I did not make it quite clear. it was just reporting the code.
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

philfrei

Re: About Question enthuware.ocajp.i.v7.2.1040 :

Post by philfrei »

This is kind of interesting:

Code: Select all

		int z = 1;
		z += (z=4) + (z=7) + z;
		System.out.println("z=" + z);
Prints: z=19

Code: Select all

		int z = 1;
		z += (z=4) + (z=7 + z);
		System.out.println("z=" + z);
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.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1040 :

Post by flex567 »

From your answer:
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.
From the expression a = a + (a=4)
Why isn't 2nd 'a' from left set to 4 instead of 10 ?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1040 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests