Java Operators
In this Chapter we are going to learn about
• What is an Operator?
• Operands
• Types of Operators
• Arithmetic Operators
• Relational Operators
What is an Operator?
• An Operator is a symbol that operates on one or more
arguments to produce result. Operators are key to
building large expressions in Java.
• Operands are the Values on which the operators act
upon.
Variable Operator
Operand
Types of Operators
• Java provides a rich set of operators to manipulate variables. We can
categorize all the commonly used Java operators into the following groups :
• Arithmetic Operators
• Relational Operators
• Assignment Operators
• Bitwise Operators
• Logical Operators
• Increment and Decrement Operators
Arithmetic Operators
• The arithmetic operators are used to construct mathematical
expressions as in algebra. Their operands are of numeric type.
• Arithmetic operators follow the same precedence rules as in
mathematics. BODMAS is sometimes called PEMDAS in some
countries which is basically the same.
• Brackets, Order, Division, Multiplication, Addition, Subtraction (BODMAS)
• Parenthesis, Exponents, Multiplication, Division, Addition, Subtraction
(PEMDAS)
Arithmetic Operators List
Assume integral variable A holds 20 and variable B holds 10, then:
Operato Description Example
r
+ Addition – Adds values on either side of the operator A + B will give 30
- Subtraction – Subtracts right hand operand from the left A – B will give 10
hand operand
* Multiplication – Multiplies values on either side of the A * B will give 200
operator
/ Division – Divides left hand operand by right hand operand A / B will give 2
% Modulus – Divides left hand operand by right hand operand A % B will give 0
and returns remainder
Relational Operators
• A Relational operator compares two values and determines the relationship between them.
• The result will be a boolean value ( true or false )
Relational Operators List
Assume integral variable A holds 10 and variable B holds 20, then:
Operato Description Example
r
== Equal to – Checks if the values of two operands are equal or ( A == B ) is not true .```
not , if yes then condition becomes true .
!= Not equal to – Checks if the values of two operands are equal or ( A != B ) is true .
not , if values are not equal then condition becomes true.
> Greater than – Checks if the value of left operand is greater than (A > B ) is not true .
the value of right operand , if yes then condition becomes true .
< Less than – Checks if the value of left operand is less than the (A < B ) is true .
value of right operand , if yes then condition becomes true .
>= Greater than or equal to – Checks if the value of left operand is (A >= B ) is not true .
greater than or equal to the value of right operand , if yes then
condition becomes true .