|
From: ankur on 4 Jul 2008 15:30 int[] z = {10,20,30,40,50}; int index = 4; z[index] = index = 2; System.out.println(z[0]); System.out.println(z[1]); System.out.println(z[2]); System.out.println(z[3]); System.out.println(z[4]); This code gives 10 20 30 40 2 Why does it not give: 10 20 2 40 50 Why z[2] is not assigned 2?? How can this be explained in terms of associativity and precedence rules.
From: Eric Sosman on 4 Jul 2008 15:37 ankur wrote: > int[] z = {10,20,30,40,50}; > int index = 4; > z[index] = index = 2; > > System.out.println(z[0]); > System.out.println(z[1]); > System.out.println(z[2]); > System.out.println(z[3]); > System.out.println(z[4]); > > > > This code gives > > 10 > 20 > 30 > 40 > 2 > > Why does it not give: > 10 > 20 > 2 > 40 > 50 > > Why z[2] is not assigned 2?? How can this be explained in terms of > associativity and precedence rules. Associativity and precedence cannot explain it. Evaluation order, though, can and does. Try printing the value of `index' before and after the `z[index]=' line, and maybe things will start to make more sense. -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: Mark Space on 4 Jul 2008 15:59 ankur wrote: > Why z[2] is not assigned 2?? How can this be explained in terms of > associativity and precedence rules. Good question. I think this is the answer: <http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26.1> "If the left-hand operand is an array access expression ... then: * First, the array reference subexpression of the left-hand operand array access expression is evaluated.... " So that's the order because the JLS says it is. For assignment, a = (b = c); if "a" is an expression (?, I think "expression" is correct, the same section talks about fields too) a is evaluated first. Then "(b = c)" is evaluated. I'd assume that if "b" is an expression, it will be evaluated before "c". I'd have to look-up which one of those is precedence, and which is associativity. It may be both one or the other, too, I suppose. Finally, the assignments occur.
From: Roedy Green on 4 Jul 2008 17:27 On Fri, 4 Jul 2008 12:30:22 -0700 (PDT), ankur <ankur.a.agarwal(a)gmail.com> wrote, quoted or indirectly quoted someone who said : >z[index] = index = 2; first of all, don't use cascaded assignment operators. The only time you see them in on exams. see http://mindprod.com/jgloss/precedence.html You will learn that = in evaluated right to left. Java has a horrible hodgepodge precedence and left to right and right to left operators mainly inherited from C. -- Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
From: Joshua Cranmer on 4 Jul 2008 17:40
ankur wrote: > int[] z = {10,20,30,40,50}; > int index = 4; > z[index] = index = 2; Generated bytecode (roughly): iconst_4 push 4 on to the stack istore <index> store the top of the stack (4) into index aload <z> load z to the stack iload <index> load index (4) on the stack iconst_2 push 2 on to the stack dup duplicate the top element (2) on the stack istore <index> store the top of the stack (2) into index iastore store the top of the stack into the index denoted by the second value on the stack of the array denoted by the third item of the stack. In general, an operand is resolved before any of the operands to the right of it and after any operand to its left, such that an assignment to its right does not affect any usages of the variable to its left. Left and right are defined, of course, in terms of the JLS and are more properly before and after, but human notation makes left/right clearer. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth |