File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 42965
+1 for the subject.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15080

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: 46320
This question arises frequently. We have an FAQ about it.
I agree. Here's the link:
subject: ++
It's not a secret anymore!