• Post Reply Bookmark Topic Watch Topic
  • New Topic

Postfix unary operator  RSS feed

 
Udvigna Prakash
Greenhorn
Posts: 15
2
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi fellow Ranchers,

I am facing difficulty in understanding below code, please help me.

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two operators in line 1. Explain exactly what each one does.
 
Udvigna Prakash
Greenhorn
Posts: 15
2
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Udvigna Prakash
Greenhorn
Posts: 15
2
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!