aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operator precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operator precedence" Watch "operator precedence" New topic
Author

operator precedence

Venkata Saraswathi
Ranch Hand

Joined: Sep 27, 2008
Posts: 55


In #1 statement ma[1] will be 1 first, then ma[1] will be incremented.
so, I think ma[1] will be 2. But I got the output 1 from line #2. how the evaluation done here.
Also, as 2+1 = 3, I thought 3 will be the output of #3. But it is 5.

can any body please explain how the compiler evaluates this expression?

--
Venkata
Narendhiran Nagarajan
Ranch Hand

Joined: Jun 17, 2009
Posts: 30
The index of array starts from 0. So, ma[0]=1,ma[1]=3,ma[2]=3,ma[4]=4

ma[ma [1] ] = ++ma[1] + (ma[1]=1); // #1

=> ma[3] = ++3 + (1) ; // now ma[1]=1 not 3
=> ma[3] = 4+1=5


Naren
-------------------
SCJP - 90%
SCWCD - 98%
Venkata Saraswathi
Ranch Hand

Joined: Sep 27, 2008
Posts: 55
ma[ma [1] ] = ++ma[1] + (ma[1]=1); // #1

=> ma[3] = ++3 + (1) ; // now ma[1]=1 not 3
=> ma[3] = 4+1=5

According to operator precedence rools expression in () will be evaluted fist then ++ will be evaluated.
So, By following that a[1] will be initialized first then its value will be incremented

=> it should be = ++1+(1) // here ma[1]=2 yes?
why should we consider like ++3 + (1) instead of above.

Please clarify
Narendhiran Nagarajan
Ranch Hand

Joined: Jun 17, 2009
Posts: 30
This link wouldhelp you more.

Operator Precdence
Venkata Saraswathi
Ranch Hand

Joined: Sep 27, 2008
Posts: 55
Thanks,

--
Venkata
Madhu Desai
Ranch Hand

Joined: Jun 14, 2009
Posts: 42
Venkata Saraswathi wrote:
=> it should be = ++1+(1) // here ma[1]=2 yes?
why should we consider like ++3 + (1) instead of above.

Please clarify


First of all let me remind you Big Daddy of all Algebra Rule
LHS = RHS

1. Forget right hand side, first only deal with LHS - you will get ma[3].
2. OK now move to RHS - of course anything with in () is computed first. But who is computing in (). Its only assigning. You know assigning will be last.
3. So now come back to ++ma[1] which will be 4. Now it is time to assign 1 in ma[1] and retrive what is in ma[1], which is 1. Add it to 4 to become 5.

Now LHS = RHS rule
ma[3] = 5;

hope i am clear



Thanks
Preparing for SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operator precedence