Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Postfixed increment operator in expressions  RSS feed

 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have tried the following expression:

the result surprises me. In fact I expected 21, it seems to me that the postfix hasn't been valued. Any suggestion about it?

Thank you in advance.
 
Roel De Nijs
Sheriff
Posts: 11077
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pagano monello wrote:the result surprises me. In fact I expected 21, it seems to me that the postfix hasn't been valued. Any suggestion about it?

Because it's postfix, the value of a (10) is used first and then it's incremented with 1 (a=11) and then the result of the sum (20) is assigned to a. It's much more clear if you use another variable to store the sum like in this example

And this topic has a very informative discussion about post-increment (or post-decrement) operators. So if you have some doubts, it's definitely worth reading. And if you still have some doubts after reading that topic, just let us know by hitting the reply button

Hope it helps!
Kind regards,
Roel

PS. You are using the postfix increment operator in your example, but the title suggests the decrement operator. I updated the title accordingly, hope that's ok for you.
 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,
thank you for your answer, I am gonna read the topic you suggested me; this thing is a little tricky.
Thank you also for having updated the title of the topic which was actually wrong.

pagano
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!