Increment operator: ++ decrement operator: -
The basic meaning: make up a variable is a number 1, or have a variable drop a number 1
using the format: written before the variable name, or write after the variable name. For example: ++ num, num ++ can also
use:
- Alone : without any other operations mixing, themselves become a separate step.
- Mix : mixing and other operations, such as mixing with an assignment, or mixed with a printing operation, and the like.
Use difference:
- When used alone, the front and rear ++ ++ does not make any difference. That is: ++ num; and num ++; is exactly the same.
- 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]
Notes:
- Variables can only use increment, decrement operators. Constants can not be changed, it can not be used.
demand
Since the illustration / difference operator decrementing
Code
public class Demo06Operator { public static void main (String [] args) { int num1 = 10 ; // used alone, the front ++ ++ num1; System.out.println (num1); // alone, after ++ num1 ++ ; System.out.println (num1); // 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); System.out.println (num2); int num3 = 30 ; //Mixed, after ++ first variable using the original 30, and then let the resulting variable +1 31 is System.out.println (num3 ++ ); System.out.println (num3); int Num4 = 40 ; // and assignment mixing, before -, then the variable immediately results to immediately -1 result1 variable int result1 = - Num4; System.out.println (result1); System.out.println (Num4); int Num5 = 50 ; // mixed use, after - first the original digital to result2, then I become myself again -1 new results int result2 = num5-- ; System.out.println (result2); System.out.println (Num5 ); int X = 10 ; int Y = 20 is ; // . 11 = + 20 is 31 is int result3 = X + ++-y - ; System.out.println (result3); System.out.println (X); System.out.println (Y); // 30 ++ ; // error writing! ++ constants can not be used or - } }
Results of the