Operators in Java
Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
1) Unary Operator,
2) Arithmetic Operator,
3) Shift Operator,
4) Relational Operator,
5) Bitwise Operator,
6) Logical Operator,
7) Conditional or Ternary Operator
8) Assignment Operator.
1. Unary Operator
The Java unary operators require only one operand. Unary operators are used to perform
various operations i.e.:
o incrementing/decrementing a value by one
o negating an expression
o inverting the value of a Boolean
Unary Operator Example: ++ and --
1. public class OperatorExample{
2. public static void main(String args[]){
3. int x=10;
4. System.out.println(x++);//10 (11)
5. System.out.println(++x);//12
6. System.out.println(x--);//12 (11)
7. System.out.println(--x);//10
8. }
2. Arithmetic Operators
Java arithmetic operators are used to perform addition, subtraction, multiplication, and
division. They act as basic mathematical operations.
Arithmetic Operator Example
1. public class OperatorExample{
2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. System.out.println(a+b);//15
6. System.out.println(a-b);//5
7. System.out.println(a*b);//50
8. System.out.println(a/b);//2
9. System.out.println(a%b);//0
10. }}
Output:
15
5
50
2
0
3. Left Shift Operator
The Java left shift operator << is used to shift all of the bits in a value to the left side of a
specified number of times.
Left Shift Operator Example
1. public class OperatorExample{
2. public static void main(String args[]){
3. System.out.println(10<<2);//10*2^2=10*4=40
4. System.out.println(10<<3);//10*2^3=10*8=80
5. System.out.println(20<<2);//20*2^2=20*4=80
6. System.out.println(15<<4);//15*2^4=15*16=240
7. }}
Output:
40
80
80
240
4. Relational operator
Relational Operators in Java are used to comparing two variables for equality, non-equality,
greater than, less than, etc. Java relational operator always returns a boolean value - true or
false.
Java has 6 relational operators.
1. == is the equality operator. This returns true if both the operands are referring to the same
object, otherwise false.
2. != is for non-equality operator. It returns true if both the operands are referring to the
different objects, otherwise false.
3. < is less than operator.
4. > is greater than operator.
5. <= is less than or equal to operator.
6. >= is greater than or equal to operator.
5. Logical operator
The logical && operator doesn't check the second condition if the first condition is false. It
checks the second condition only if the first one is true.
1. public class OperatorExample{
2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int c=20;
6. System.out.println(a<b&&a<c);//false && true = false
7. System.out.println(a<b&&a<c);//false & true = false
8. }}
Output:
false
false
6. Bitwise operator
The bitwise | operator always checks both conditions whether first condition is true or false.
1. public class OperatorExample{
2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int c=20;
6. System.out.println(a>b||a<c);//true || true = true
7. System.out.println(a>b|a<c);//true | true = true
8. //|| vs |
9. System.out.println(a>b||a++<c);//true || true = true
10. System.out.println(a);//10 because second condition is not checked
11. System.out.println(a>b|a++<c);//true | true = true
12. System.out.println(a);//11 because second condition is checked
13. }}
Output:
true
true
true
10
true
11
7. Ternary Operator
Java Ternary operator is used as one line replacement for if-then-else statement and used a lot
in Java programming. It is the only conditional operator which takes three operands.
Ternary Operator Example
1. public class OperatorExample{
2. public static void main(String args[]){
3. int a=2;
4. int b=5;
5. int min=(a<b)?a:b;
6. System.out.println(min);
7. }}
Output:
8. Assignment Operator
Java assignment operator is one of the most common operators. It is used to assign the value
on its right to the operand on its left.
Java Assignment Operator Example
1. public class OperatorExample{
2. public static void main(String args[]){
3. int a=10;
4. int b=20;
5. a+=4;//a=a+4 (a=10+4)
6. b-=4;//b=b-4 (b=20-4)
7. System.out.println(a);
8. System.out.println(b);
9. }}
Output:
14
16