Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Joined: Jun 20, 2002
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
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
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.
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
Joined: Aug 26, 2001
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.