Skip to main content

How should postincrement work?

Posted by rdtindsm on July 29, 2009 at 5:09 PM PDT

Let me acknowledge that thhis is more or less a cross post from another thread.

The original poster asked why the expression
i=10;
i=i++; evaluates to 10 rather than 11.

In the discussion, the answer is that the assignment is made before i is incremented. I to would expect the expression to evaluate as 11.

referring to http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#4987
**************************************
15.14.2 Postfix Increment Operator ++

PostIncrementExpression:
PostfixExpression ++

A postfix expression followed by a ++ operator is a postfix increment expression. The result of the postfix expression must be a variable of a type that is convertible (

Comments

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: How should postincrement work?

Hello,
Post increment increments the value of the variable by one after the execution to the statement. It means that if we write “i++” then the value of the “I” will increment after its execution.
Regards
Data Recovery
http://www.datadoctor.biz