You should know the evaluation order of an expression. this order is from left to right. use your example: 1. int [ ] a = new int[ 2 ]; 2. int b = 1; 3. a[b] = b = 0; at line 3,first a[b] is evaluated.now b is 1,so a[b] is a. now you can see it's a that is being assighned a value. hope this may clear your doubt. Bin Zhao
This is the same example explained in RHE chapter 2. The game is all about order of evaluatig expression and operator precedence and associativity. In this example value will be stored in a and that to 0. final expression will become a = 0; How ? Beacause in java all expressions are evaluated first from left to right. so after this we will have a -> a b -> b (just a reference) 0 -> 0 (literal) so now expression becomes a = b = 0; now associativity of oprators comes in to play. = operator is evaluated right to left so expression reduces to a = b (where now b = 0) so finaly 0 is assigned to a
Originally posted by sudha: [B]Hi This is from deepak's mock Consider the following code :
1. int [ ] a = new int[ 2 ]; 2. int b = 1; 3. a[b] = b = 0;
Value will be stored in what element of array a. A.a B.none C.a Answer given was a How can it be a.I didn't get this.Can any of you please explain. Regards Sudha
That was the wonderful explanation Rajesh. Where did u get it. Actually I want to delve a bit more into evaluation of expressions. I knew the funda but couldn't get it documented anywhere. Hope to hear from u soon.
Hey guys!! come and have some cups of HOT JAVA........<BR>Adeesh