Introduced
Arithmetic operators are used in mathematical expressions, and they function in the same way as they do in mathematics. The following table lists all the arithmetic operators.
The instance in the table assumes that the value of integer variable A is 10, and the value of variable B is 20:
operator |
Description |
Example |
+ |
Values on both sides of the addition-add operator |
A + B equals 30 |
- |
Subtraction-left operand minus right operand |
A–b equals-10 |
* |
Multiply-multiplies the values on both sides of the operator |
A * b equals 200 |
/ |
Division-left operand divided by right operand |
b/a equals 2. |
% |
Residual-left operand divided by the remainder of the right operand |
B%a equals 0. |
++ |
Self-increment: The value of the operand is increased by 1 |
b++ or ++b equals 21 (see below for differences) |
-- |
Self-Subtract: The value of the operand is reduced by 1 |
b--or--b equals 19 (see below for differences) |
Instance
Public classArithmeticoperator { Public Static voidMain (string[] args) {intA = 10; intb = 20; intc = 25; intD = 25; System.out.println ("A + b =" + (A + b));//addition OperationSystem.out.println ("A-B =" + (a));//Subtraction OperationsSYSTEM.OUT.PRINTLN ("A * b =" + (A * b));//Multiplication OperationsSystem.out.println ("b/a =" + (b/a));//Division OperationSystem.out.println ("b% A =" + (b% a));//take the remainder operationSystem.out.println ("c% A =" + (c% a));//System.out.println ("a++ =" + (a++));//self-increment, first output + + +System.out.println ("a--=" + (a--));//self-subtraction, first output A and then a--。 Since the previous step a outputs since the increment, so at this time a=11,System.out.println ("C + + =" + (c + +));//first output, then self-incrementSystem.out.println ("++d =" + (++d));//first self-increment, then output }}
Operation Result:
A + b = ten-B = -10* b =/a =2 A = 0% A = 5a+ + =a
-- = onec+ + = 25++d = 26
Attention:
1. BYTE/SHORT/CHAR automatically promoted to int when calculating
2. The integer type remains unchanged at the time of operation
3. Small type and large type operation, the result must be large type (for example: Byte is a small type, int is a large type)
4. Arbitrary integer/0-arithmeticexception-arithmetic anomalies
Any non-0 digit/0.0 any non 0 decimal/0 = Infinity-Infinity
0/0.0
0.0/0.0 =NaN = not a number + = non-numeric
0.0/0
Java operators, arithmetic operators