*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on operator precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on operator precedence" Watch "Question on operator precedence" New topic
Author

Question on operator precedence

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
I hope this is helpful.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Jul 02, 2002
Posts: 1865
The operators *,/, and % all have the same precedence. In any expression that includes % and * just evaluate from left to right.
Ian Wayne
Greenhorn

Joined: Aug 03, 2002
Posts: 8
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
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ian Wayne
Greenhorn

Joined: Aug 03, 2002
Posts: 8
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

Joined: Aug 26, 2001
Posts: 7610
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on operator precedence
 
Similar Threads
Question on GC
regard on k&b book
Number comparison...incorrect result
Missed by 1 Q
Chapter 12 Coffee Cram