++ increment, decrement - Operator

Operator action - ++,

  • ++: from Canada . The original data +1
  • -: decrement. The original data -1

Using the format : written before the variable name, or write after the variable name. For example: i ++ or ++ i

Use:

Alone : without any other operations mixing, to become a separate step. When the independent variable operation, the front and rear ++ ++ no difference.

Mixing operations: and other operations mixing, for example mixing with the assignment, the printing operation of the hybrid, etc.

  • Former ++ / Front -  : So variables at once immediately + 1 / -1, and then took the results of the use of
  • After ++ / rear -  : Well first of all use the original value of the variable, and then let the variable + 1 / -1

Code Example

public  class Demo05 {
     / * 
increment operator: ++ 
decrement operator: - 

basic meaning: a variable make up a number 1, so that a variable or a drop number 1 
using the format: written before the variable name, or write after the variable name. For example: ++ num, num ++ may be 
used: 
    1. alone: any other operations and do not mix themselves as a separate step. 
    2 in combination: mixing and other operations, such as mixing with an assignment, or mixed with a printing operation, and the like. 
Use difference: 
    1. When used alone, the front and rear ++ ++ does not make any difference. That is: ++ num; and num ++; is exactly the same. 
    2. In the mixing time, there are significant differences [] 
        A. If it is before [++], then the variable immediately immediately [+1], and then use the results hold. [Use] the first increase after 
        B. If the [++], then the first to use the original value of the variable, and then let the variable [+1]. After the first use [plus] 

Note: 
    Only variables to use the increment, decrement operators. Constants can not be changed, it can not be used. 
* / 

    Public  static  void main (String [] args) {
         int num1 = 10 ;
        System.out.println (num1); // 10 
        ++ num1; // used alone, the front ++ 
        System.out.println (num1); // . 11 
        num1 ++; // used alone, after ++ 
        the System.out. the println (num1); // 12 is
         // mixed with the printing operation time 
        int num2 = 20 is ;
         // mix, first ++ 21 immediately become variable at once, then print the results 21 
        System.out.println (+ num2 ); // 21 is 
        System.out.println (num2); // 21 is 
        System.out.println ( "=================" ); 

        int num3 = 30 ;
         / /Mixed, after ++ first variable using the original 30, and then let the resulting variable +1 31 is 
        System.out.println (num3 ++); // 30 
        System.out.println (num3); // 31 is 
        int Num4 = 40 ;
         // operation of the mixing and assignment 
        int result1 = --num4; // mixed, pre -, -1 immediately immediately become variable 39, 39 and then to the result variable result1 
        System.out.println (result1); / / 39 
        System.out.println (Num4); // 39 
        int Num5 = 50 ;
         // mix, after -, the first original digital result2 to 50, and again I -1 becomes 49 
        int result2 = num5-- ; 
        System.out.println (result2); // 50
        System.out.println (Num5); // 49 
        int X = 10 ;
         int Y = 20 is ;
         // . 11 = + 20 is 31 is 
        int result3 = X + ++-y - ; 
        System.out.println (result3); / / 31 is 
        System.out.println (X); // . 11 
        System.out.println (Y); // . 19 

        // 30 ++; // error writing! ++ constants can not be used or - 
    } 
}

 

Guess you like

Origin www.cnblogs.com/wurengen/p/10991588.html