Hello everybody!

I'm learning Java programming and have a really, really dumb question to ask.

When writing a line with a counter, such as:

for (var count = 0; count < aString.length; count ++)

what does the ++ do? I've seen it in lots of code and it is mentioned in my book, but what does it actually do?

Cheers!

Dani AI

Generated

Quick, practical answer for Java: the ++ operator adds one to a variable, but its exact behavior depends on whether it’s used as a postfix (i++) or prefix (++i). pointed this out — below are concise Java examples and the important gotchas that follow.

int a = 5;
int b = a++;   // b == 5, a == 6
int c = ++a;   // a == 7, c == 7

int x = 0;
x = x++;       // x remains 0 — the post-increment returned the old value which got assigned back

Rules and pitfalls to note:

  • The operand must be something you can assign to (a local variable, field, or array element). You cannot write (a + b)++ or 5++.
  • ++ works on primitive numeric types and char. It also appears to work on wrapper types (e.g., Integer) because of autoboxing, but Integer n = null; n++; throws a NullPointerException.
  • i++ is not atomic in a multi-threaded context. Concurrent increments must use AtomicInteger.incrementAndGet() or proper synchronization to avoid race conditions.
  • About performance: for Java primitives the JVM/JIT typically makes i++ and ++i equally efficient in practice. Choose the one that makes your code clearer. (In other languages or for user-defined iterators, pre-increment can matter.)

Recommendation: use i++ in simple loops for readability, avoid multiple increments of the same variable inside a single expression, and be careful with autoboxed types and concurrency. This keeps behavior predictable and prevents the subtle bugs that catch beginners (and grizzled pros) alike.

Recommended Answers

All 3 Replies

It's called "incrementation." It's something that has been in programming for a very long time. Even as low level as assembly language, has a special opcode for incrementing variables (INC in asm). This is the process of simply adding one. So, if the value of count is 1, then after the count++ the variable count becomes 2. Higher level languages, such as BASIC (in all forms that I know of) do not use this method. You have to explicitly say: count = count + 1. Also, WHERE you put the ++ becomes important in some languages also. If you put the ++ after (count++) the variable name, then it adds 1 to the variable AFTER whatever condition is tested. If you were to put ++ BEFORE the variable (++count) then it would increment (add one) to the variable BEFORE the condition is tested. Hope that wild explanation has helped a little.

It's called "incrementation." It's something that has been in programming for a very long time. Even as low level as assembly language, has a special opcode for incrementing variables (INC in asm). This is the process of simply adding one. So, if the value of count is 1, then after the count++ the variable count becomes 2. Higher level languages, such as BASIC (in all forms that I know of) do not use this method. You have to explicitly say: count = count + 1. Also, WHERE you put the ++ becomes important in some languages also. If you put the ++ after (count++) the variable name, then it adds 1 to the variable AFTER whatever condition is tested. If you were to put ++ BEFORE the variable (++count) then it would increment (add one) to the variable BEFORE the condition is tested. Hope that wild explanation has helped a little.

This helps a lot, thanks. Just to maintain the dumb question ideal, can I use count = count + 1, and it does the same job?

Cheers!

The Simple Answer: Yes.

for (i=0; i<100; i = i +1) {
     document.writeln(i + "<BR>");
}

Works (tested in firefox) just fine. The thing with that though... is that if you are concerned about programming speed and power, using var = var + 1 is NOT the way to go. As a matter of programming ethics, and as a matter code effeciency (on a very low level, it takes more time to process i = i + 1, than it does to process i++, but even at a low level, a bunch of i = i + 1's can add up). The best way to add one to a variable is to use ++. The only reason you shouldn't use ++, is if the programming language you are using doesn't support it (like visual basic).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.