 Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Paul Clapham
• Jeanne Boyarsky
• Knute Snortum
Sheriffs:
• Liutauras Vilda
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
Bartenders:
• Joe Ess
• salvin francis
• fred rosenberger

# operator precedence

Greenhorn
Posts: 17
• • • • 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;
a[a[b]]=a[b]=b=2;
for(int i=0;i<4;i++)
{
System.out.println(a[i]);
}
}
}
regards
durga.

Greenhorn
Posts: 16
• • • • 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; //a=0, a=0, a=0, a=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]=a=b=2
//then further evaluation:
//a=0=b=2
//where you set a 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
• • • • 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
also it decides a[a[b]] is a,
second : =a[b], because b is 0 ,a[b] just a
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 so a[b]=b just a=2
the last : a[a[b]]=a[b], as it decided a[a[b]] is a
so this assignment just as a[a]=a, which the same as a =a
which means a=2
The result as a = 2 the other 0 as before.

Ranch Hand
Posts: 68
• • • • 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
2) a[b] evaluates to a
3) assignment is evaluated performed right to left
4) b = 2 assigns 2 to b and returns 2 to a[b]
5) a = 2 assigns 2 to a and returns 2 to a[a[b]]
6) a = 2 assigns 2 to a 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 = 2, a = 0, a = 0, a = 0 are printed out.

durga krishna
Greenhorn
Posts: 17
• • • • Hi Maja,nan and huing li
Thanx for ur detailed explanations.hi nan (fyi)i found this in a mock exam.
durga. I was her plaything! And so was this tiny ad: Java file APIs (DOC, XLS, PDF, and many more) https://products.aspose.com/total/java