programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Bear Bibeault
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Jj Roberts
• Carey Brown
Bartenders:
• salvin francis
• Frits Walraven
• Piet Souris

# Post increment and Pre Increment Precedence

Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
I am studying for SCJP and using Khalid Mughal as my first main book. I was working on operators and i got into the following problem :

According to KHALID MUGHAL book chapter no.3 page no 82 the precedence table states that POSTFIX OPERATOR have highest precedence followed by unary prefix.

If i work according to this rule the

i=10
a = i++ + ++i + i--;
a = 10 /*i became 11 now */ + ++i + 11 // now i became 10
a = 10 + 11 +11 = 32

output is 32. But actual output is 34.

In COMPLETE REFERNCE the precedence of both PREFIX AND POSTFIX is same.

Which one shall i follow ? and Kindly correct me if i am wrong in above problem.
[ August 05, 2008: Message edited by: Gaurav Chauhan ]

Gaurav Chauhan
Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

http://www.uni-bonn.de/~manfear/javaoperators.php

http://www.cs.uwf.edu/~eelsheik/cop2253/resources/op_precedence.html

The difference in precedence in above mentioned links

author and iconoclast
Posts: 24203
44
• Number of slices to send:
Optional 'thank-you' note:
There are three terms in the sum. The value of the first term is 10, the value of the postincrement operator. "i" is then incremented, so its value is 11. Then the value of the second term is 12; that's the value of the preincrement operator (11 + 1), and i is now 12. The sum after two terms is 22. Then you add i--, which is also 12: again, the postdecrement operator returns the unmodified value. 10 + 12 + 12 is 34.

[ Gaaah! Fixed typo ]
[ August 05, 2008: Message edited by: Ernest Friedman-Hill ]

Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
Ya interesting problem , It has to be 32 , guess this may be a bug in JVM implementation.

Ranch Hand
Posts: 82
2
• Number of slices to send:
Optional 'thank-you' note:

Ernest Friedman-Hill said "the postdecrement operator returns the unmodified value. 10 + 12 + 12 is 32. "

I assume that 10+12+12 is still 34.
Larry

Ernest Friedman-Hill
author and iconoclast
Posts: 24203
44
• Number of slices to send:
Optional 'thank-you' note:
Sorry. Fixed my typo

Ranch Hand
Posts: 44
• Number of slices to send:
Optional 'thank-you' note:
Try Head First Java for tutorials,complete refrence is ok but khalid mugal is quite brief as per for a beginner.Ernest Friedman-Hill's reply is solution to your problem.

Gaurav Chauhan
Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
This concept really solves the problem and gets me the exact result.

But if we look at the precedences on SUN WEBSITE link given above. That says that postfix is at the top of list followed by unary prefix. Is this wrong there ?

One more problem try this folks:

int x =7, y =10;

System.out.println( x++ - y++ + ++x + ++y) ;

x++ - y++ + ++x + ++y
7 - 10 + 9 + 12
18
It works according to the rule given by the member.

 I am going down to the lab. Do NOT let anyone in. Not even this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton