Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on operator precedence

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i=1;
i += ~i - -i * ++i + i-- % ++i * i++;
System.out.println(i);
o/p is 1
This was the question in Dan's mock exam by topic.
Can somebody explain how?
Thanks
Veena
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this is helpful.
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan,
How the expression(2%2*2) results in 0?
* operator has higher precedence than % operator right?I am sorry if I am wrong...I don't have clear idea about how operators of same precedence are handled...
Thanks for replieng with patience,even when question is silly.
Veena
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Veena, % * have the same precedence, and it is evaluated from left to right (or left associativity). 2%2 is 0 so...
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The operators *,/, and % all have the same precedence. In any expression that includes % and * just evaluate from left to right.
 
Ian Wayne
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question about precedence:
operator . has higher precedence than operator new, but we use the following statement a lot:
new SomeClassName().someMethod();
It seems 'new' is operated before '.'. Why?
Thanks.
Ian
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian,
new is not an operator it is only a keyword used in primary expressions.
As you can see JLS 3.12 Operators does not list the keyword new as being an operator.
Also, '.' (the period) is not an operator but what the JLS calls a separator (see JLS 3.11 Separators). So there is no such thing as precedence as far as separators and the new keyword are concerned. '.' is always used in qualified expressions and new in primary expressions.
 
Ian Wayne
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Valentin
Then Table3.1 (p.42) of Mughal's book is not precise. It lists new and () as operators. So is there any section in JLS where precedence and associatity of operators are specified? I couldn't find it.
Ian
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian,
there is no special section in the JLS dealing with precedence and stuff, everything is a little bit scattered everywhere (as in any spec ). JLS 15.7 Evaluation Order contains some of those precedence rules but for the rest you have to browse through JLS 15.14 Postfix Expressions and JLS 15.28 Constant Expression to find relevant information.
Bottom line: if you don't want to spend time reading the JLS, take the Mughal's precedence table as is and just make the difference between operators and separators when reading it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic