• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using increaments ie (post)i++ (pre) ++i  RSS feed

 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

I do not understand the reason why I get the following results. With the pre-increment I get a result of 12345.

With the post-increment, I'm getting 00000.

I saw this in linkin and there were a lot of confusion on why it is that way. I think this is a Computer Science thing. It is very interesting topic.

Here's the code:
 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way thank you in advanced!!!

your friend,

Bill Foster
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this FAQ page: Post Increment Operator and Assignment.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in postincrement -> n++ value of n will be first assigned to other variable and then n's value will be incremented
in pre-increment -> ++n value of n will first incremented and then n's value will be assigned to other variable

taking this into consideration, you can find answer to your query

~ abhay
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. The value of i is incremented first in i++, but the value of the whole expression i++ is equal to the old value of i. It is this old value that you are assigning to.
 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dear friends,

Actually I think we should use the term assigns more than equals because we must think that the line of code is doing stuff on the right side then assigns it on the left(two part execution) .

The post (i++) assigns then increments after assignment. On the pre (++i) the value is incremented and then assigned to the variable. On the J.L.S. Java Language Specification it says this.

Here's the site:

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#24924

you need to do this :



This makes it more readable to the person who needs to maintain the code and using documentation javadoc to tell what the class is doing.

What do you guys think?

Bill Foster
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill foster wrote:
Dear friends,

Actually I think we should use the term assigns more than equals . . .

The post (i++) assigns then increments after assignment. . . .
I used "equals" very carefully. The ++ operator does not assign anything. It does two things, which can be clearly seen from the Java™ Language Specification (JLS) link you posted. It increments the value of the operand, and sets the value of the expression equal to the old value of the operand. It says nothing about "assign" in the JLS section.

I used "assigning to" badly: sorry. It would be better had I said "assigning to i".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!