Originally posted by Vivek Gupta:
Q1 -> the precedence of [] operator is higher than = so a should been seen as a[0] in this example and not a[10]
Hi Vivek
First of all
[] is not operator. check the link below from JLS.
http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#230663 Now as RHE says:
[I] All operands are evaluated left to right,
even if the order of execution of the operations is something different.
here in Q1.
<pre>
a
=i=10;-------------explan this line
</pre>
while evaluating opearnds, when we move from left to right. First we get a[i], 'i'(index of a) is evaluated and replaced by it's value 0.
second we get 'i' which is simply reference to variable called i.
third we get '10' which is nothing but a integer litral.
[I]Now evaluation is over, now operations will take place. and your statement is right now is like this:
a[0] = i = 10;
now u are right as associativity of operator '=' is from RIGHT to LEFT.
(a[0] = (i = 10));
henec a[0]= 10 , this will not throw any error/exception and print 10 7.
Q 2: This might help you :
http://www.javaranch.com/campfire/StoryBits.jsp ------------------
Regards
Ravish