Difference Between Prefix Increment and Postfix Increment Operators

Figure 4.15 demonstrates the difference between the prefix increment and postfix increment versions of the ++ increment operator. The decrement operator (--) works similarly.

 1   // Fig. 4.15: Increment.java 2   // Prefix increment and postfix increment operators. 3  4   public class Increment 5   { 6      public static void main(String[] args) 7      { 8         // demonstrate postfix increment operator 9         int c = 5;10         System.out.printf("c before postincrement: %d%n", c); // prints 511         System.out.printf("    postincrementing c: %d%n", c++); // prints 5 12         System.out.printf(" c after postincrement: %d%n", c); // ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.