Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Operator Precendence

 
ali_the_great_one
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Bruesch
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic