Doesn't the (a=b) have to be evaluated at the beginning of the execution of this line?
If it does, doesn't it assign the value in b to a?
And, if it did wouldn't we be picking up b instead of a ?
My head is really fuzzy on this one. But it seems like the reassignment of a in the parentheses would hava an effect on the final a since a was set equal to b.
[Update] I prototyped this and ran it and it does give the answer 1.
However, if you add an extra line that merely prints out a guess what I got? 2.
So the re-assignment DID happen but apparently due to some rule about WHEN the assignment takes place with regard to the evaluation of the REST of the variables on this line, the change does not take place until after the line executes... that is... there is a deferral in actually updating a with the contents of b. I guess as the line was scanned, a was already picked up as a from the left, so the assignment in the middle of the line didn't change it? I dont know but it looks to be so. [ October 20, 2008: Message edited by: Bob Ruth ]
SCJP - 86% - June 11, 2009
Well these types of questions are not asked in the real exam(I hope so :roll: ) as they are minute details which are given in mock exams just to make you feel that the mock is very good while actually these type of things are irrelevant...
Swati are you talking about real exam questions. If yes then let me tell you that it is illegal to use these type of questions as they are illegally copied thus are illegal. Don't waste your SCJP if you have not given it yet. Using these questions anyone can pass the exam. Then it will be of no use...
no anderson this is what me and bob are talking about
Joined: Oct 10, 2008
Aaah, sorry got confused with the post by Anut.
Anyway, the reason for this is that the line
System.out.println( a [ (a = b) ] );
is evaluated as an expression. An expression is a syntactic construction that has a value. Expressions are formed by combining variables, constants, and method returned values using operators. Java expressions can have side-effects, which are actions that are executed when the expression is evaluated. The assignment of (a=b) is a side effect of the evaluation of the statement, therefore when you go on to then look at a, the assigment has taken place.
Well I really want this thread to end as I think that the question is just a waste as these type of questions are not asked in the exam (I think so as most of the standard mock exams don't have these type of question)
But anderson it's true that the expression a [ (a = b) ] has side effects, but the evaluation order must be like this (as you yourself explained)
a[ (a=b) ] //a and b are now the same arrays. Here I feel that a=b will evaluate to a a[ a ] // a evaluates to 0 a //how does this evaluates to 1 ??
Joined: Oct 10, 2008
a [ (a = b) ] // (a=b) resolves to b! a [ b ] // b resolves to 0 a [ 0 ] = 1
However, as a Side Effect of this evaluation the reference variable a now points to the array b, hence after this expression when you look at a this is the same as looking at b, the answer being 2.
i am not yet a certified candidate.(preparing for scjp 6) so correct me if i am wrong.
manoj kumar jena
Joined: Sep 24, 2007
in the first System.out.println() statement its not a its a. sorry i was in bit hurry to post the answer.
Joined: Sep 20, 2008
According To all Programming Language First Inner Most "()" Will execute in this case (a = b) should execute First so reference b must come to a...so both refer to same object... so b.. which means b reference to index 3 value is 0
now both refer to same object the out put must be 2 (a = b)