programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Postfix unary operator

Udvigna Prakash
Greenhorn
Posts: 15
2
Hi fellow Ranchers,

int a=10;
a=a++;   //Line 1
int b = a;
System.out.println(a+ " "+b );

Above code produces 10, 10.  Why doesn't value of 'a' gets increased after line 1?

Regards

Stephan van Hulst
Saloon Keeper
Posts: 7993
143
There are two operators in line 1. Explain exactly what each one does.

Udvigna Prakash
Greenhorn
Posts: 15
2
Hi Stephen,

I am trying to write my understanding, please correct, wherever I am wrong.

a=a++;   //Line 1

1.) First right hand side of "=" operator works as mentioned below
a++ first assigns a's value to a temporary variable(say, "temp"), then it plans to increases the value of a by 1, once the expression "a++" is over.

2.) Now "=" operator works and assigns the value of "temp" to a, thus ending the statement as "a=10";

3.) Here is where I am confused, after ";" is reached in line 1, shouldn't a's value be incremented by 1 ?

Regards

Stephan van Hulst
Saloon Keeper
Posts: 7993
143
Udvigna Prakash wrote:thus ending the statement as "a=10";

after ";" is reached in line 1, shouldn't a's value be incremented by 1 ?

This is a contradiction. Which of the two is it? When the statement ends, does a = 10 or is a incremented by 1?

Udvigna Prakash
Greenhorn
Posts: 15
2
As per my understanding, "thus ending the statement as a=10" means before just before ";" is encountered, a is assigned the value of 10. However as soon as ";" is encountered, now the statement has officially ended and before any next line is executed, a's value should be incremented by 1. I believe I am not clear on the part, when does the postfix increment becomes redundant in a statement.

I know, any postfix operator increases the operand's value by 1, if it encounters the operand again. e.g,
int a=10;
a= a++ + a;  // This is actually a=10 +11;
System.out.println(a);
This prints 21, which is OK.

BUT
int a=10;
a= a + a++; //Line 2: This is actually a =10+10;
System.out.println(a);  //Line 3
Why doesn't a gets incremented after line 2 is executed and before Line 3 could execute?

Regards

Stephan van Hulst
Saloon Keeper
Posts: 7993
143
Side effects of operators take place before the operator returns the result of the operation.

In a = a++, a is incremented by 1 before the result is returned. You then assign the result to a. In effect, you've incremented a by 1, and then you reset it back to 10.

In the second expression, instead of assigning the old value directly to a, you first add the new value of a to it. That's why it seems the second example is okay.

Henry Wong
author
Sheriff
Posts: 23295
125

Udvigna Prakash
Greenhorn
Posts: 15
2
Thank you Henry and Stephen.

I think the bottom-line here is whenever post-fix operator is used along with assignment operator and the variable on the left side is same as operand,the incremented value gets lost.

Thank you Henry for sharing other posts, they helped to understand this better.

Regards

Fred Kleinschmidt
Bartender
Posts: 571
9
And these discussions point out why you should never write statements where a single variable is modified more than once in that statement.
While the result is explicitly defined by Java syntax, it can be very confusing to follow.

And if someone else attempts to modify the code at a later date, that person will probably not know whether the original author knew what was going to happen.

Note that in other languages with similar syntax (such as C and C++) those statements produce undefined behavior and can produce seemingly arbitrary results.

Junilu Lacar
Sheriff
Posts: 11494
180
The line that goes below your bottom line is: Don't take examples like that as examples of how to write good code. They are not, so do not do as they do. In fact, never do as they do.

Udvigna Prakash
Greenhorn
Posts: 15
2
Thank you Fred and Junilu,

I completely agree with what you guys mentioned. Actually this came up during my preparation for OCAJP8, so thought of getting help from fellow ranchers. I am aiming to clear the exam before May end :-)

Regards

 I agree. Here's the link: http://javaranch.com/programming-pearls.jsp