Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operator presedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operator presedence" Watch "operator presedence" New topic
Author

operator presedence

Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400



I found it in jqplus.


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Expression is evaluated to x = (x) +(AN_INT+(++x))
that's why output is 16


Sage of The Monstrous Toad of Mount Myoboku
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

the lass ++x wont effect the actual value of x??
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
i think its because of ( x += AN_INT)+((++x))) becasue +comes first in the hierarchy
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


here x is 84....according to K&B right of += is calculated first...

so minhaj i think your answer should be x+=3+7; which is x=7+10...17

then why 16? hmmmm
Chandana Garlapati
Ranch Hand

Joined: Jul 06, 2009
Posts: 97
Minhaj kaimkhani wrote:

Hope its clear now..
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
then why is x*=7+5; not x=x*7+5; ?
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
maybe its becasue its x*(7+5) becasue of that rule agian
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Chandana Garlapati wrote:
Minhaj kaimkhani wrote:

Hope its clear now..
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
maybe its becasue the operands are evaluated from left to right....

i think this is x+= (AN_INT) + (++x).....first += is opened up soit is

its x=(x) + (AN_INT) + (++x)

this is simply so confusing
Chandana Garlapati
Ranch Hand

Joined: Jul 06, 2009
Posts: 97
Assignment operators have right to left associativity.i.e; the expression is evaluated from right to left.
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
so the output should be 17 right Chandana?
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
No, the answer is 16.

when post increment is applied second time the value is stored temporarily and after all calculations final value is assigned to x.


SCJP 1.6 96%
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
neha can you explain in detail?
Chandana Garlapati
Ranch Hand

Joined: Jul 06, 2009
Posts: 97
ones at the right should be performed before the ones at the left.
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
ok so correct me if am wrong

here x is 6;
x+=3 + ++x;

so Chandana according to you it would be x+=3+ 7; which evaluates to x+=10;

which is x=x+10;

which is x=10+7; right? or 10+6?
Chandana Garlapati
Ranch Hand

Joined: Jul 06, 2009
Posts: 97
As Neha said, value is stored temporarily and after performing the right expression then x value is added...

lets try this


Now the output is 17..
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
leave it...why dont either of you simply show me the calculation step by step...the answer is 16....maybe Ankit or Henry could explain properly
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
ok..expression is x+= AN_INT + ++x which is x = x + AN_INT + ++x
now according to the rule right side will be calculated first but from left to right
so it becomes: 6+ 3 +7=16.

now, I hope its clear.

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
i just read the jls and there it is mentioned that the program rememebrs what you had on the left hand side...so its like x+=3+7 whcih gives x+=10 which is x=x+10 equals x=6+10 which is 16......Bingo

understood Ankur???


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operator presedence