permaculture playing cards*
The moose likes Beginning Java and the fly likes About Operator Precendence 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 » Java » Beginning Java
Bookmark "About Operator Precendence" Watch "About Operator Precendence" New topic
Author

About Operator Precendence

ali_the_great_one
Greenhorn

Joined: Sep 06, 2001
Posts: 1
hello
i just wanna know that in the following java code which operator has the highest precedence
int h = 5 , y ;
y = ++h + h++ + (h++ + 5);
system.out.println(y);
there i use bracket and a postfix operator inside it. now my question is what will computer do first it deals with brackets or it deals with postfix operator.
thank you
ali asghar
------------------
Argm Mastoi
Ranch Hand

Joined: Oct 16, 2001
Posts: 35
I think first the compiler will go inside the bracket and solve the matter inside it means first add the values and then increase the value of h and then will solve the rest of th problem.
Please correct me if i'm wrong, b-cos this just my opinion.
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
If I'm not mistaken, the output of this program would be 24. First the variables inside of the parentheses are evaluated first, (h++ + 5) which equals 10 and h now equals 6 (the value is used first, then the variable is incremented). Then the prefix operator is evaluated on h making h 7 now. Then it adds the 7 plus another 7 making 14. h is incremented after evaluating. And 14 + 10 equals 24.
Here are the steps:
h = 5;
y = ++h + h++ + (h++ + 5);
//
y = ++h + h++ + (5 + 5);
// h = 6 now
y = ++h + h++ + 10;
y = 7 + h++ + 10;
// h = 7 now
y = 7 + 7 + 10;
// h = 8 now
y = 24
Hope that's clear...
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
http://www.geocities.com/mjbruesch


Michael J Bruesch<br /><i>I code, therefore I am.</i>
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
Almost forgot, here's a good table for operator precedence if you want to check it out:
http://stono.cs.cofc.edu/~manaris/spring01/cs220-handouts/Java_operator_precedence.htm
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
http://www.geocities.com/mjbruesch
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
ali_the_great_one
Hi, and welcome to the ranch. The javaranch has thousands of visitors here all the time and has established a naming policy to help try to make this site a more professional forum for everyone. Your name is not in keeping with that policy, please read it and change your name to comply with this policy. Basically you should use your real first and last name with a space in the middle.
Thanks for your cooperation and please dont be a stranger to the ranch.

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: About Operator Precendence