Java programming those things 27-other operators Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb
4.6 other operatorsOperators that cannot be classified, or that occupy a specific class are described below. L ++ and -- are the increment and decrement operators in the program. For more information, see the following sample code: int n = 0; n ++; // n = n + 1 system. out. println (n); N ++ adds 1 to the original N value and then assigns it to itself, because the original N value is 0, after 1 is added, it becomes 1. Similarly, the meaning of the decimal operator is the same. Example code: int m = 0; m --; system. out. println (m); the meaning of M-is to subtract 1 from the original variable M and then assign it to itself, then the value of M is changed to-1. Note that ++ and-can only operate on variables, but cannot operate on other content. The following statements are incorrect: int A = 0; int B = 0; (a + B) ++; // error final int M = 1; m ++; // error 5 ++; // error in actual writing, ++ and-can be written either before a variable or after a variable, for example, int K = 0; k ++; ++ K; similarly, -- or, so what are the differences in actual use? In fact, there is no difference in the value of the variable, that is, whether it is written or before, the value of the variable must be increased by 1, regardless of whether it is written after or before, the values of all variables are reduced by 1. The biggest difference is the value of the entire formula, such as N ++. The rule is as follows: 1) ++ or-before the variable, then the value of this formula is equal to the value after the variable changes. 2) If ++ or-is written after the variable, the value of this formula is equal to the value before the variable changes. The sample code is as follows: int n = 1; int M = 1; n ++; // The value of N is changed to 2 ++ m; // The value of M is changed to 2 int K = n ++; // if the initial value of N is 2, the value of N ++ is 2, and the value of N is 3, the value of K is 2 Int J = ++ m; // if the initial value of M is 2, the value of ++ m is 3, and the value of M is 3, the value of J is 3, which is the same as that. The following is an example of a slightly comprehensive point: int A = 0; int B = 0; A = B ++; // A is 0, B is 1 A = ++ B; // A is 2, B is 2 B = A ++; // A is 3, B is 2 A = + + B; // A is 3, B is 3 Description: The comments are the values of A and B after the corresponding line of code is run. In program development, this difference can be used to simplify code writing, but this is not recommended because it increases the difficulty of reading the code. L + and-the addition and subtraction operators are introduced earlier. In fact, the addition and subtraction operators + and-have another meaning, that is, they represent positive and negative. Generally, the positive number can be omitted, the negative sign can be combined with values, variables, and calculation formula. The sample code is as follows: int A = 0; int B = 1; int c =-5; C =-; C =-(A + B); L?: This operator is called a conditional operator. Its function is to obtain the corresponding value based on the judgment result. The syntax format is as follows: conditional? Value 1: Value 2 syntax requires that the conditional part must be of the boolean type, which can be a Boolean value, a Boolean variable, or a formula formed by a relational or logical operator, values 1 and 2 must be converted to the same type. Function Description: if the result of the conditional expression is true, the value of the entire formula is 1; otherwise, the value of 2. The sample code is as follows: int x = 10; int y = 20; int max = x> Y? X: Y; // If X is greater than Y, take the value of variable X and assign it to Max int A =-10; int ABS = a> 0? A:-A; // implement the absolute value evaluate function L () brackets, which is also an operator. The function is to make the calculation inside the brackets first, which is consistent with that in mathematics, it is only the legal formula that can be used in the program code. Sample Code: int A = 1 + 2*3; int A = (1 + 2) * 3; // different from the running results of the above Code, each operator has its own priority. Brackets can be used to increase the priority of the corresponding formula. The concept of operator priority will be introduced later.