Array

public class lost { public static void main (String [] args){ int [] array = new int[10]; int i=5; array[i++]=++i+i++; System.out.println(array[5]+" "+array[6]+" "+i); } } When i tried to execute this code,i got answers to be as 14 0 8. Could anybody answer how it could be 14 0 8. because i think that array[i++]=++i+i++;for int i = 5 is array[5]=6+5=11. array[6]=? i=?

I am reproducing a code help analyse the additions. public class lost { public static void main (String [] args){ int [] array = new int[10]; int i=5; array[i++]=++i+i++;

/* i++ in the array subscript makes the value of i as (5+1=6) then pre subscript ++i further increments it to (6+1=7) this (7) is added to its present value which is also (7) , making a addition as 14 and this is the value of array[5]. array[6] is zero bcos of default initialisation. and value of i is (i+1; i.e. 7+1 = 8)... Hence printout is 14 0 8 */ System.out.println(array[5]+" "+array[6]+" "+i); } }

Hi Alkesh, I tell you a simple(easy) method to solve this type of situations. Just follow these rules . You will get the answer correct. 1. wherever you find the var just substitute the value and when you find ++/-- operator irrespective of post/pre increment/decrement (++/--) blindly increment/decrement the latest value of that var.. You have to start from left to right. So let's start

int i=5; array[i++]=++i+i++; array[5(6)] = (7)7 + 7(8); (i) for i substitute the latest value of i (ii) for ++ symbol, increment the latest value (iii) for -- symbol, decrement the latest value (iv) Finally do the calculation for all non-bracketed values (v) Be careful when the assigned var is the same as the var which undergone all these ++/-- operations in the statement. In this case the assigned var takes its latest value int i = 10; ex. i = i++ + --i; i = 10(11) + (10)10; So i = 10 after exe of above stmt *** Notice there is an error here, it should be 20. It is corrected below *** (vi) In order to do the arithmetic, justcut-down all the bracket values

array[5] = 7 + 7; //Note here i's latest value is 8 array[5] = 14;

(vi) Since all the elements of an int[] has default value 0,array[6] is 0. System.out.println(array[5]+" "+array[6]+" "+i); So it prints 14 0 8

Hi, anna: Nice comment! But when I run the following piece of code, the result is 20 in stead of 10. Vivian (v) Be careful when the assigned var is the same as the var which undergone all these ++/-- operations in the statement. In this case the assigned var takes its latest value int i = 10; ex. i = i++ + --i; i = 10(11) + (10)10; So i = 10 after exe of above stmt

<BR>Vivian

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467

posted Mar 17, 2000 14:44:00

0

Yes you are correct Vivian. What happened was while I was writing that post, I wanted to insist on the foll. statementi = i++; . This example was already preset well before in my mind . And in course of writing instead of this example and just cut and pasted some other line. I am sorry for this. Always the arithmetic is done only on unbracketed values and the result is assigned to the left hand side. Since the left hand side var happened to be the var i itself it takes the calculated value. But the above said easy ( ) methods works as it is. Thank you Vivian once again. regds maha anna

Originally posted by Vivian, Song:Hi, anna: Nice comment! But when I run the following piece of code, the result is 20 in stead of 10. Vivian (v) Be careful when the assigned var is the same as the var which undergone all these ++/-- operations in the statement. In this case the assigned var takes its latest value int i = 10; [b] ex. i = i++ + --i; i = 10(11) + (10)10; So i = 10 after exe of above stmt [/B]

Accoriding to MA's explaination, i have tried to solve it.I hope u can understand. i=10 i++ + i-- : 10(11)+11(10)=10+11=21 The last value of i is 10. ++i + --i : (11)11+(10)10=11+10=21 The last value of i is 10. i-- + i++ : 10(9)+9(10)=10+9=19 The last value of i is 10. --i + ++i : (9)9+10(10)=9+10=19 The last value of i is 10. --i + i++ : (9)9+9(10)=9+9=18 The last value of i is 10. ++i + i--) : (11)11+11(10)=11+11=22 The last value of i is 10. i-- + ++i : 10(9)+10(10)=10+10=20 The last value of i is 10. i++ + --i : 10(11)+(10)10=10+10=20 The last value of i is 10. Basanti

The placement of the parenthesis is a little different from what a person would normally write. The reason for the difference is the fact that the JVM processes the expression from left to right. The JVM can not look at the entire expression as we can. While a human can start the evaluation of an expression from the inner most parenthesis at any point in the expression--left, right, or center--the JVM must start parsing the expression on the left and work toward the right. The difference between the human approach and the machine approach only becomes an issue when unary operators produce side effects. That's why we humans frequently have problems with expression involving unary operators with side effects. The key to success is to make sure that the unary operators are evaluated from left to right. Once you have evaluated the unary operators with side effects (i.e. increment and decrement) there is no need to write the parenthesis as the JVM would view the expression. After the side effects are evaluated the result of the human view and the JVM view are the same. You could then write the parenthesis in a more human readable form as follows.

Originally posted by Kelvin Michael: array[i++]=++i+i++; Howdy, my question is why the i in the array will be execute at first? Anyone can explain it, thx.

The left hand operand of the simple assignment operator is always evaluated before the right hand operand. The JVM always works through an expression from left to right. Here's another example.

Prints: [a,0],[b,1],1,0,0,2 The array index expression is evaluated before the right hand operand of the simple assignment operator. Evaluation of the array index expression causes method m to be invoked. The input parameter is the postfix increment expression with the operand i. Since the postfix increment expression returns the original value of the operand, the value zero is passed to method m along with a String containing the character a. Method m returns the value that was passed in as an input parameter. In this case, the return value is zero. As a side effect of the postfix increment expression, the value of variable i is one after the array index expression is evaluated. The right hand operand of the simple assignment operator is the return value of method m. The input parameter to method m is the postfix increment expression with variable i as the operand. The value of variable i is now one and that is the value that is passed into method m and that is the value returned by method m and assigned to element zero of the array a. As a side effect of the postfix increment expression, the value of variable i is two after method m is invoked.

let me rephrase my question regarding MA's methodology. When MA says blindly how blindly? Am I right in assuming that we dont ignore the priority of operators? Can we ignore the left and right associativity?