SCJP Study Guide:
Fundamentals


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents
XyzWs SCJP Study Guide: Increment and Decrement Operators

Increment and Decrement Operators


Increment and decrement operators can be applied to all integers and floating point types. They can be used either prefix (--x, ++x) or postfix (x--, x++) mode.


Prefix Increment/Decrement Operation


The operand of a prefix increment or decrement operation must be an expression classified as a variable. The result of the operation is a value of the same type as the operand.

int x = 2;
int y = ++x;	// x == 3, y == 3

The run-time processing of a prefix increment or decrement operation of the form ++x or --x consists of the following steps:

  • x is evaluated to produce the variable.
  • The selected operator is invoked with the value of x as its argument.
  • The value returned by the operator is stored in the location given by the evaluation of x.
  • The value returned by the operator becomes the result of the operation.

Postfix Increment/Decrement Operation


The ++ and -- operators also support postfix notation. The result of x++ or x-- is the value of x BEFORE the operation, whereas the result of ++x or --x is the value of x AFTER the operation. In either case, x itself has the same value after the operation.

int x = 2;
int y = x++;	// x == 3, y == 2

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |