Operators in Java are represented by the symbols. These symbols are (+,-,*,/,||,++,–,==,%,!=) etc.
In Java, operators are used to performing operations between operands for example A+B. Here A and B are the Operands and (+) is the operator. that is performing addition operation.
Mainly, Operators in Java are divided into 8 types according to the use or the common property of operations.
Types of Operators in Java
- Arithmetic operators
- Increment/decrement operators(++,–)
- Relational operators
- Logical operators
- Shift operators
- Bitwise operators
- Assignment Operators
- Ternary operator
Arithmetic Operators in Java
Arithmetic operators are used to performing all the mathematical operations in Java. Like addition, subtraction, multiplication, division, and remainder which are +,-,*,/ and most important %.
Each of these operators is a binary operator, it requires two values (operands) to calculate a final answer.
Example: Suppose, P=20 Q=10,
We have two variables or operands P and Q now let’s see how to perform arithmetic operators in java on these two operands.
p + q = 30 p - q = 10 p * q = 200 p / q = 2 p % q = 0
Increment and Decrement operators in Java
As the name suggests Increment operator(++) is used to increments the value of operand by one and the ++ symbol is used to define the increment operator in Java.
Example: If p=10 Then p++ or ++p = 11
Similarly, Decrement Operator(–) is used to subtract one from the value of the operand and — symbol is used to define the decrement operator in Java.
Example: If p=10 Then p– or –p = 9
Did you notice there are two types of Increment or decrement operators? This is called Pre Increment and Post increment and similarly Pre decrement and post decrement.
Prefix version: the value of the operand is first changed (increment or decrements ) and then changed value is used in the expression.
Postfix version: the unchanged value of the operand is first used in the expression and then the value of the operand is updated.
Let’s check some examples In detail on the Increment and decrement operators in Java.
Relational operators in Java
Relational operators in Java are used to compare two values or operands Like (A>B) or (10>20) and the statement will return result in the boolean form True or False.
Java offers six relational operators for comparing numbers and characters. but they don’t work with strings. If the comparison is true, the relational expression results in the Boolean value true and false, if the comparison is not true.
Example: Suppose p = 2 and q = 6 then,
- p<q = true
- p<=q = true
- p==q = false
- p>q = false
- p>=q = false
- p!= q = true
Logical operators in Java
Logical operators are used to perform condition between two operands or values Like &&, ||, ! and these operators also called conditional operators.
Java supports six conditional operators – five binary and one unary.
Shift operators in Java
A shift operator in Java usually performs bit manipulation on data by shifting the bits of its first operand right or left.
>: shifts bits of op1 to right by distance op2(signed shifting).
<<: shifts bits of op1 to lefts by distance op2(signed shifting).
>>>: shifts bits of op1 to right by distance op2(unsigned shifting).
Bitwise operators in Java
The bitwise operators in Java is used to perform manipulation in the bit of a number
Java provides following bitwise operators
- &( bitwise AND)
- ^ (eXclusive_OR i.e., XOR)
- (OR) operations and ~(bitwise complements)
Assignment Operators in Java
Assignment operators in Java are used to assign a value to any operands, for example, int a = 20. Here = is the assignment operator. That assigning a value to a.
int age; age = 27;
Java provides more assignment operators to write the code in a clean and short form.
We will see the assignment operator in detail in a separate tutorial.
Ternary/Conditional operator in Java
Java offers a shortcut conditional operators (?:) that stores a value depending upon a condition. this operator is a ternary operator I.e., require three operands. the general form of conditional operators ?: is as follows:
expression1 ?expression2 : expression3
Example: 6>4?7:8 evaluates to 7 because test expression 6 > 4 is true.