Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I don't get the concept of Increment Operators

 
Papa Miller
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when it comes to prefix and postfix increments I guess that I understand it when a value is by itself it doesn't matter which side the operator is on

for example A=7; A++; A value is now 8
or A=7; ++A; A value is now 8

but the way I am reading this, there is a difference in how these two terms operate

A++ means "use the current value of A first and then increment A"
++A means "increment A first and then use it"

and now I get

A=7;
B=A++;
The Value of B is 7, and the value of A is 8.

Maybe somebody can shed some light on this??? I am able to write a little program that returns the expected answer, but I still don't understand exactly what is going on here! It says right there that A= 7, how can A be 8?
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you incremented A using A++. To see the difference, write something along the following:



You will see that in the first s.o.p statement, the value 1 is printed for a, then a is incremented because the second s.o.p. statement prints 2. For the third s.o.p. statement you will see that b is incremented first, as it prints 2. The fourth s.o.p. statement is there to reinforce that b was incremented before printing in the third statement.

 
Papa Miller
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks W. Joe,
I tried that and I see that it prints out
1
2
2
2

so your statement makes sense. I am starting to understand it better. I will definitely play around with it some and put some different variables in there.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where this really starts to make a difference is when you have more complicated expressions

The expressions:

7 + a++

and

7 + ++a

will give you different results.
 
Stephan van Hulst
Bartender
Pie
Posts: 6113
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my humble opinion, I think the incremental and decremental operators should never be used as part of a larger statement, with the exception of the for-loop.

Always put ++a / a++ / --a / a-- on a new line. Never evaluate them in an expression.
 
Papa Miller
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Where this really starts to make a difference is when you have more complicated expressions

The expressions:

7 + a++

and

7 + ++a

will give you different results.


Yeah, this it the part that really throws me being totally new to programming. To take Algebra, you assign one value to a variable, period. So here you assign a value to a variable, then something else comes along and changes it, plus there's a question of exactly WHEN does it change. So now I am paranoid b/c a value that I thought I could depend on forever has changed, and I don't know if it can keep changing? Also, depending on when you look at the variable, it might be one thing, or it might be the other. You could blink your eyes and miss it. Is it going to change back when I'm not looking? I don't know. I have some trust issues I guess.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49782
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you search you will find questions like this come up frequently. People forget that a++ means there is a value for a and also a value for the whole expression a++, and on that particular line, the two values are different.
There are many people who do use those operators as part of an expression; you can use them in array indices like this
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
programs would be pretty boring if the value of a variable could never change. and after all, the word 'variable' implies that it could...well...VARY.

if it is important that the value not change, there are ways you can 'lock' it down so that it can never change - and by that, I mean NEVER change. But even then there are subtleties you need to be aware of (eventually, not as a beginner).

One GOOD thing about programming is that the rules are solid and well defined (most of the time)



 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I asked a similar question recently in this forum, did a bit of reading and wrote up an explanation, if your still intersted in this topic check out my blog post
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic