aspose file tools*
The moose likes Beginning Java and the fly likes I don't get the concept of Increment Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I don Watch "I don New topic
Author

I don't get the concept of Increment Operators

Papa Miller
Greenhorn

Joined: Sep 22, 2010
Posts: 11
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

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Papa Miller
Greenhorn

Joined: Sep 22, 2010
Posts: 11
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

Joined: Oct 02, 2003
Posts: 11479
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Sep 22, 2010
Posts: 11
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

Joined: Oct 13, 2005
Posts: 39851
    
  28
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

Joined: Oct 02, 2003
Posts: 11479
    
  16

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

Joined: Dec 21, 2007
Posts: 228

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


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I don't get the concept of Increment Operators