Introduction to Arithmetic Operators
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.
The following table lists the arithmetic operators:
|
Operator |
Result |
|
+ |
Addition |
|
- |
Subtraction |
|
* |
Multiplication |
|
/ |
Division |
|
% |
Modulus |
|
++ |
Increment |
|
+= |
Addition assignment |
|
-= |
Subtraction assignment |
|
*= |
Multiplication assignment |
|
/= |
Division assignment |
|
%= |
Modulus assignment |
|
-- |
Decrement |
The operands of the arithmetic operators must be of a numeric type. You cannot use them on boolean types, but you can use them on char types, since the char type in Java is, essentially, a subset of int.
Syntax
operand1 = operand2 + operand3; // Use to add values
operand1 = operand2 - operand3; // Use to subtract values
operand1 = operand2 * operand3; // Use to multiply values
operand1 = operand2 / operand3; // Use to divide values
operand1 = operand2 % operand3; // Use to get modulus
operand++; // Use to add 1 in operand
operand1 += operand2 ; // Use to add operand2 in operand1
operand1 -= operand2 ; // Use to subtract operand2 from operand1
operand1 *= operand2 ; // Use to multiple operand2 with operand1
operand1 /= operand2 ; // Use to divide operand2 with operand1
operand1 %= operand2 ; // Use to get modulus of operand1
operand--; // Use to decrement of 1 from operand value
Example
1. /**
2. * @(#)ArithmeticOperatorDemo.java
3. *
4. *
5. * @author
6. * @version 1.00 2009/11/2
7. */
8.
9. public class ArithmeticOperatorDemo {
10.
11. /**
12. * Creates a new instance of ArithmeticOperatorDemo.
13. */
14. public ArithmeticOperatorDemo() {
15. }
16.
17. /**
18. * @param args the command line arguments
19. */
20. public static void main(String[] args) {
21. // TODO code application logic here
22.
23. int a = 25;
24. int b = 10;
25.
26. int c = a + b;
27.
28. System.out.println("Addition: " + c );
29.
30. int d = a - b;
31.
32. System.out.println("Subtraction: " + d );
33.
34.
35. int e = a * b;
36.
37. System.out.println("Multiplication: " + e );
38.
39.
40. int f = a / b;
41.
42. System.out.println("Division: " + f );
43.
44.
45. int g = a % b;
46.
47. System.out.println("Modulus: " + g );
48.
49.
50. a++;
51.
52. System.out.println("Increment: " + a );
53.
54.
55. a--;
56.
57. System.out.println("Decrement: " + a );
58.
59. a += b;
60.
61. System.out.println("Addition assignment: " + a );
62.
63.
64. a -= b;
65.
66. System.out.println("Subtraction assignment: " + a );
67.
68. a *= b;
69.
70. System.out.println("Multiplication assignment: " + a );
71.
72.
73. a /= b;
74.
75. System.out.println("Division assignment: " + a );
76.
77. a %= b;
78.
79. System.out.println("Modulus assignment: " + a );
80.
81. a--;
82.
83. System.out.println("Decrement assignment: " + a );
84.
85. }
86. }
Output
Addition: 35
Subtraction: 15
Multiplication: 250
Division: 2
Modulus: 5
Increment: 26
Decrement: 25
Addition assignment: 35
Subtraction assignment: 25
Multiplication assignment: 250
Division assignment: 25
Modulus assignment: 5
Decrement assignment: 4
