Arithmetic and Relational Operators in Java
In this class, We discuss Arithmetic and Relational Operators in Java.
The reader should have proper knowledge of data types and variables. Click here.
Operator: An operator is a symbol in java used to perform some operations.
Arithmetic operators:
Addition symbol +
Subtraction symbol –
Multiplication symbol *
Division symbol /
Modulus symbol %
Arithmetic operators are binary operators.
Example: a+b, the symbol is used between two operands.
a, and b are operands.
The below diagram shows the sample program.
c= a+b
The value present in a is added to the value present in b.
The result of addition is stored in c.
Similarly, subtraction is done using a-b.
Multiplication using a*b.
Division should be understood in deep.
The division is done using a/b.
20/30 = 0.66
The value 0.66 should be stored in variable c.
Important: c is a variable of type int.
The value 0.66 is a floating point value.
The value present before the dot, i.e., 0 in our example, is stored in variable c.
The output a/b is 0.
Modulus:
The modulus operator gives the reminder value.
Example:
a = 5 and b = 2
a%b = 1
5/2 remainder is 1.
We use modulus in many of the coding examples.
Relational operators:
less than <
less than or equal to <=
greater than >
greater than or equal to >=
instance of
equal to ==
not equal to !=
The below diagram shows an example code.
a < b will display true.
The relational operators will give true or false output.
The relation a<b is true because 2<5 is true.