Help coderanch get a
new server
by contributing to the fundraiser
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Devaka Cooray
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• paul wheaton
• Henry Wong
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Carey Brown
• Mikalai Zaikin
Bartenders:
• Lou Hamers
• Piet Souris
• Frits Walraven

# operator precedence

Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
hi all
i have a doubt in the following.the output is 2,0,0,0.
can someone explain in detail .thanx for ur help.
class goin
{
public static void main(String args[])
{
int b=0;
int a[]=new int[4];
a[a[b]]=a[b]=b=2;
for(int i=0;i<4;i++)
{
System.out.println(a[i]);
}
}
}
regards
durga.

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
The expressions are always evaluated from left to right.
I added some comments so I hope that it will be more clear this way.
int b=0;//b =0
int a[]=new int[4]; //a[0]=0, a[1]=0, a[2]=0, a[3]=0
a[a[b]]=a[b]=b=2;
Hope that this will help
//when you strat evaluating from left you get at first you get:
//a[a[0]]=a[0]=b=2
//then further evaluation:
//a[0]=0=b=2
//where you set a[0] to be 0 and b to be 2
for(int i=0;i<4;i++)
{
System.out.println(a[i]);
}
}
}
regards
durga.

Ranch Hand
Posts: 167
• Number of slices to send:
Optional 'thank-you' note:
First of all, if you don't want to quit the job and till be able to
get benifits from government( like here in Australia), please
do not write code like that! or next lay-out you will
be the first candidate.
There are two thing involved in this problem, one is
Evaluation order(see R&H page 33) and the other is the
Assignment Operators(see R&H page 65)
Evaluation order said that JAVA scan a line of code from
left to right and decide what it is,
Assignment Operator run from right to left.
so when it sees this line of code :a[a[b]]=a[b]=b=2;
Evaluation first(from left to right)
first : a[a[b]]; as at right now b = 0, so a[b] is a[0]
also it decides a[a[b]] is a[0],
second : =a[b], because b is 0 ,a[b] just a[0]
third : b just b
the last : is 2
now it is time Assignment Operator(from right to left)
first : b=2, so now b is 2
Second : a[b]=b, it decided a[b] is a[0] so a[b]=b just a[0]=2
the last : a[a[b]]=a[b], as it decided a[a[b]] is a[0]
so this assignment just as a[a[0]]=a[0], which the same as a[0] =a[0]
which means a[0]=2
The result as a[0] = 2 the other 0 as before.

Ranch Hand
Posts: 68
• Number of slices to send:
Optional 'thank-you' note:
This reference should be useful:
15.13.2 Examples: Array Access Evaluation Order http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#23902
a[a[b]] = a[b] = b = 2; is evaluated as follows
0) The left-hand operand of a binary operator is evaluated first
1) a[a[b]] evaluates to a[0]
2) a[b] evaluates to a[0]
3) assignment is evaluated performed right to left
4) b = 2 assigns 2 to b and returns 2 to a[b]
5) a[0] = 2 assigns 2 to a[0] and returns 2 to a[a[b]]
6) a[0] = 2 assigns 2 to a[0] again and returns 2
No specific assignment is made to the other three array elements, so they still have the default int value of zero.
Therefore, a[0] = 2, a[1] = 0, a[2] = 0, a[3] = 0 are printed out.

durga krishna
Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
Hi Maja,nan and huing li
Thanx for ur detailed explanations.hi nan (fyi)i found this in a mock exam.
durga.

 Don't get me started about those stupid light bulbs.
reply
Similar Threads