The following table lists all arithmetic operators in Java.
All operators listed in Table can only be used with numeric type operands.
Operators | Description | Type | Usage | Result |
---|---|---|---|---|
+ | Addition | Binary | 2 + 5 | 7 |
- | Subtraction | Binary | 5 - 2 | 3 |
+ | Unary plus | Unary | +5 | Positive five. Same as 5 |
- | Unary minus | Unary | -5 | Negative of five |
* | Multiplication | Binary | 5 * 3 | 15 |
/ | Division | Binary | 5 / 2 | 2 |
% | Modulus | Binary | 5 % 3 | 2 |
++ | Increment | Unary | num++ | Evaluates to the value of num, increments num by 1. |
-- | Decrement | Unary | num-- | Evaluates to the value of num, decrements num by 1. |
+= | Arithmetic compound-assignment | Binary | num += 5 | Adds 5 to the value of num and assigns the result to num. If num is 10, the new value of num will be 15. |
-= | Arithmetic compound assignment | Binary | num -= 3 | Subtracts 3 from the value of num and assigns the result to num. If num is 10, the new value of num will be 7. |
*= | Arithmetic compound assignment | Binary | num *= 15 | Multiplies 15 to the value of num and assigns the result to num. If num is 10, the new value of num will be 150. |
/= | Arithmetic compound assignment | Binary | num /= 5 | Divides the value of num by 5 and assigns the result to num. If num is 10, the new value of num will be 2. |
%= | Arithmetic compound assignment | Binary | num %= 5 | Calculates the remainder of num divided by 5 and assigns the result to num. If num is 12, the new value of num will be 2. |
public class Main { public static void main(String[] args) { int num = 123; double realNum = 123.45F; double veryBigNum = 123.4 / 0.0; double garbage = 0.0 / 0.0; boolean test = true; System.out.println("num = " + num); System.out.println("realNum = " + realNum); System.out.println("veryBigNum = " + veryBigNum); System.out.println("garbage = " + garbage); System.out.println("test = " + test); System.out.println("12.5 + 100 = " + (12.5 + 100)); System.out.println("12.5 - 100 = " + (12.5 - 100)); System.out.println("12.5 * 100 = " + (12.5 * 100)); System.out.println("12.5 / 100 = " + (12.5 / 100)); System.out.println("12.5 % 100 = " + (12.5 % 100)); System.out.println("\"abc\" + \"xyz\" = " + "\"" + ("abc" + "xyz") + "\""); }/*from w ww .j a v a 2 s . c o m*/ }