Increment and decrement operator

Java has increment and decrement operator pretty much like C, C++ and C# and other languages. They do behave predictably the same too.

You can copy the code above and save it to a file named IncDec.java, then compile and run it as

$ javac IncDec.java
$ java IncDec

Just note the behavior of postfix and prefix operator when applied to expressions. In the example code above, a still results to 0 even if wrote ‘x = a++’, the assignment of the current value of a (which is zero) has been assigned to x first, then a’s value has been recalculated/incremented. While in the other statement ‘y = ++b’, the value stored in the y variable is the calculated/incremented value of b.

Be careful when using the increment/decrement operators.

If you want to show appreciation for my efforts dear reader, you could buy me a tall hazel nut Americano ($2) via PayPal. Thanks
Post navigation
(previous post)
(next post)
| | | .

{0 Comments .. you can add one }

Leave a comment