aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question on operator precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question on operator precedence" Watch "a question on operator precedence" New topic
Author

a question on operator precedence

Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
Hi guys
I find myself going back to the basics day by day.
if the postfix operator precedence is much higher than the equality (==) operator, then in the following code:
int i = 0;
while( i++ == 0 ) System.out.print(i);
System.out.print(i);
shouldn't the expression be treated as follows?
while( (i++) == 0 ) System.out.print(i) ??
BUT the the value is first compared and then incremented. The output is therefore '12'.
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104

In the post increment i++ the value of i that is used in the calculation is the original value ,increment or decrement only occurs only after the expression is calculated.
hence the output will be 1
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
A postfix operator will return the original value first before incrementing.
In while( i++ == 0 ), you have ( 0 == 0)
After this boolean statement executes, i is now equal to 1 and System.out.print(1). The while condition is now false so the next stament is executed, which is System.out.print(2);
[ August 22, 2002: Message edited by: Anthony Villanueva ]
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
Thanks Zarina & Anthony.
I understand the reason for the output. But My question was regarding the operator precedence.
The precedence of the postfix operator is higher than the equality operator. In this case, shouldn't the value be incremented before comparison? according to the precedence table?
Thanks.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
i is incremented before the comparison, but the comparison will still use the old value of i.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: a question on operator precedence