This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes ++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "++" Watch "++" New topic


Miran Cvenkel
Ranch Hand

Joined: Nov 23, 2010
Posts: 169
out of curiosity:

I get out 0, but then again i have no idea how that work at all.

gives me 1.

I would expect 1 in first case.

as it looks now:

would give same thing at start, but the code is different.

Any thought about that ?

Searchable nature photo gallery:
Stuart James

Joined: Nov 23, 2010
Posts: 4
If I am understanding your question correctly, the first call to out.Print gives 0 because you have used the postfix increment operator - the value of c is printed, then incremented.

If you want to see 1 in the first call you need to use the prefix increment operator (++c). Then the value is incremented prior to printing.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
+1 for the subject.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15002

To summarize it with code:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44609
This question arises frequently. We have an FAQ about it.
I agree. Here's the link:
subject: ++
It's not a secret anymore!