Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array [j] = array [++j] + 10; 1

Status
Not open for further replies.

prosper

Programmer
Sep 4, 2001
631
HK
The whole program is here
class plus
{
public static void main(String args[])
{
int array[] = {0,1,2,3,4,5};
int j =1 ;
array[j] = array[++j]+10;
for (int m=0; m<array.length; m++)
System.out.println(array[m]);
}
}

A book tells me to calcuate the right side ++j first and become array[2] = 12;
But the jdk1.4 shows another result
Would anyone explain it?
 
Golden rule:
When a variable appears more then once in a statement,
never use statements with side-effects on that variable
within that statement.

The sequence of calculating the whole statement can be
different from compiler to compiler.

instead of:
array[j] = array[++j]+10;

write:
j++;
array[j] = array[j]+10;


It's a lot safer!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top