Java programming those things 21 -- Arithmetic Operators Zhengzhou game institute Chen yuefeng from: http://blog.csdn.net/mailbomb
Chapter 4 OperatorsA computer, as its name implies, is a computing machine. Therefore, in programming, a large number of computation (operations) are also required. Operators are abbreviated as operators. Since computers can perform various operations, many operators are provided. Some of these operators are frequently used in reality, and many are newly added in computers. To learn operators, you must first master the operation rules of each operation, and then use the corresponding operators when appropriate. This requires familiarity with operators and basic computer knowledge. There are many types of operators. To facilitate learning, we will introduce them in the following categories.
4.1 Arithmetic OperatorsArithmetic Operators, also known as mathematical operators, are the symbols used to perform arithmetic operations. For the symbols, functions, and descriptions in the syntax, see Table 4-1 Arithmetic Operators.
Symbol |
Name |
Function Description |
+ |
Add |
Addition operation |
- |
Subtraction |
Subtraction |
* |
Multiplication |
Multiplication |
/ |
Division |
Division |
% |
Remainder |
Returns the remainder of the division of two numbers. |
In arithmetic operators, the operation rules of +,-, *, And/are basically the same as those of mathematics. In arithmetic operations, multiplication and division take precedence over addition and subtraction, and are calculated in the order from left to right, the difference is that the multiplication number in the l program cannot be omitted. in mathematics, y = 2x can be written, but y = 2 * X must be written in the program. L The type of the operation result is the same as that of the highest type involved in the operation, for example, integer plus integer or integer. Division is the most influential factor. Integer Division is an integer result or an integer. For example, the result of 10/3 is 3 rather than 3.333. The remainder operator is used to take the remainder of two numbers. For example, if 10% 3 represents the remainder of 10 divided by 3, the result is 1. The remainder operation is also widely used in programming. Common uses include controlling rule changes and controlling random numbers. The example code for basic arithmetic operators is as follows: int n = 3 + 5; int A = 10; int B = 20; int c = a * B; double D = 100.2; double d1 = d + A; In the Arithmetic Operators section, special attention is paid to the syntax Phenomenon of "promotion ". After the promotion is performed on three numeric types (byte, short, and char) lower than the int type, the result is automatically upgraded to the int type. The sample code is as follows: byte b1 = 10; byte b2 = 20; byte B3 = b1 + B2; // syntax error, Type Mismatch int n = b1 + B2; // or byte B3 = (byte) (b1 + b2); Use Arithmetic Operators in the program to perform mathematical operations in the program. Parentheses can also be added during the operation, just like in mathematics, in the program, the internal content of the parentheses is calculated first, and then the external content of the parentheses is calculated. The sample code is as follows: int A = 1; int B = 2; int c = 3; int d = C * (a + B) + C; note that the variable must be assigned a value during calculation; otherwise, a syntax error is reported, for example, int N; int m = 2 * N;