1. , -, *, / are our commonly used arithmetic operators, representing addition, subtraction, multiplication and division.
In addition, we can also use remainder: %
System.out.println(1 + 2); System.out.println(2 - 1); System.out.println(1 * 2); System.out.println(1 / 2); System.out.println(1 % 2);
2. Division does not output 1/2=0.5. Why?
Since 1 and 2 are both integers, the expected result is 0.5 floating point, and Java performs integer division.
Below we declare that one of the parameters is a floating point type, allowing the compiler to automatically convert the type.
// 打印->0.5 System.out.println(1 / 2.0);
The above is the detailed content of How to use Java arithmetic operators. For more information, please follow other related articles on the PHP Chinese website!