The ++ operators may appear before the value (prefix) or after the value (postfix). : Unary Operators « Operators « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Operators » Unary Operators 
2.2.5.The ++ operators may appear before the value (prefix) or after the value (postfix).
++x (prefix)

    x++ (postfix)

    --x (prefix)

    x-- (postfix)


Examples of Pre-and Post- Increment and Decrement Operations

Initial Value of x         Expression            y       x

5                          y = x++               5       6

5                          y = ++x               6       6

5                          y = x--               5       4

5                          y = --x               4       4

When using a prefix expression, the value returned is the value that is calculated after the prefix operator is applied. 
When using a postfix expression, the value returned is the value of the expression before the postfix operator is applied. 

public class MainClass {
  public static void main(String[] argv) {
    int x = 10;
    int y = ++x; // The value assigned to y is 11
    System.out.println(x);
    System.out.println(y);
    x = 10;
    y = x++; // The value assigned to y is 10
    System.out.println(x);
    System.out.println(y);

    x = 10;
    y = --x; // The value assigned to y is 9
    System.out.println(x);
    System.out.println(y);

    x = 10;
    y = x--; // The value assigned to y is 10

    System.out.println(x);
    System.out.println(y);
  }
}
11
11
11
10
9
9
9
10
2.2.Unary Operators
2.2.1.Unary operators take only a single operand.
2.2.2.The unary operators are used to increment, decrement, or change the sign of a value.
2.2.3.Increment and Decrement
2.2.4.+ and - operators are applied to a value of byte, char, and short types, the value is converted to an int.
2.2.5.The ++ operators may appear before the value (prefix) or after the value (postfix).
2.2.6.preincrement or pre-decrement, post-increment or post-decrement
2.2.7.mixing the increment and decrement operators with other operators
2.2.8.increment or decrement operators on a final variable.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.