Arithmetic Operators are used to perform some mathematical operations like addition, subtraction, multiplication, division, and modulo (or remainder).
Arithmetic Operators
Arithmetic Operators are used to perform some mathematical operations like addition, subtraction, multiplication, division, and modulo (or remainder). These are generally performed on an expression or operands. The symbols of arithmetic operators are given in a table:
Symbol | Name of the Operator | Example |
+ | Additive Operator | n = n + 1; |
- | Subtraction Operator | n = n - 1; |
* | Multiplication Operator | n = n * 1; |
/ | Division Operator | n = n / 1; |
% | Remainder Operator | n = n % 1; |
The "+" operator can also be used to concatenate (to join) the two strings together. For example:
String str1 = "Concatenation of the first"; String str2 = "and second String"; String result = str1 + str2; |
The variable "result" now
contains a
string "Concatenation of the first and second String".
Lets have one more example implementing all arithmetic operators:
class ArithmeticDemo{
|
Output of the Program:
C:\nisha>javac ArithmeticDemo.java C:\nisha>java ArithmeticDemo The addition of (x + y): 10 The subtraction of (y - x): 2 The multiplication of (x * y): 24 The division of (y / x): 1 The remainder of (z % x): 4 The result is now : 16 |