Java Operators are meant to perform some specific operations on the provided operands.
Say,
int num = 5 + 3;
Here, ‘=’ is the assignment operator and ‘+’ is the plus operator.
Well, the plus operator is used to add two values, ‘5’ and ‘3’. Meanwhile, the ‘=’ or equals operator assigns the value to the variable named ‘num’.
Likewise, Java offers several operators for us to use. Each carrying its own special meaning. Let’s look at them one at a time.
Types of Java Operators
Java divides the operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
Arithmetic Operators
Under this type, basic mathematical operations are performed. For instance addition, multiplication, division. etc.
Here’s is an example code for you to understand better
public class OperatorsDemo {
public static void main(String[] args) {
int a = 9;
int b = 2;
int sum = a + b;
int dif = a - b;
int mul = a * b;
int div = a / b;
int rem = a % b;
System.out.println("=====================");
System.out.println("Addition = "+ sum);
System.out.println("Subtraction = "+ dif);
System.out.println("Multiplication = "+ mul);
System.out.println("Quotient = "+ div);
System.out.println("Remainder = "+ rem);
System.out.println("=====================");
}
}
OUTPUT
=====================
Addition = 11
Subtraction = 7
Multiplication = 18
Quotient = 4
Remainder = 1
=====================
All the above operations have used 2 operators and hence they are classified as binary operators. Likewise, Unary Operators work with a single operand only. These are:
Unary Increment Operator
Denoted by the ‘++’ symbol. Used along with an operand to increase its value by 1. If used before the operand it is prefix and if used after the operand then it’s postfix.
Unary Decrement Operator
Denoted by ‘–‘ symbol. Used along with an operand to decrease its value by 1. If used before the operand it is prefix and if used after the operand then it’s postfix.
The difference between the prefix and postfix expressions is the way they associate with the rest of the program. If the prefix is used then the updated value of the variable is put to use instantly. Whereas if the postfix is used it uses the current value for that instance and updated value for later instances. Let’s look at an example to make things more clear.
public class OperatorsDemo {
public static void main(String[] args) {
int a = 9;
System.out.println("current value of a: "+ a);
System.out.println("prefix :"+(++a));//value of a is incremented then printed
System.out.println("current value of a: "+ a);
System.out.println("postfix :"+(a++));//value of a is printed then incremented
System.out.println("current value of a: "+ a);
}
}
OUTPUT
current value of a: 9
prefix :10
current value of a: 10
postfix :10
current value of a: 11
Assignment Operator
As discussed above the assignment operator ‘=’ is used to assign a value to a variable in java.
For instance,
int x = 10;
The value ’10’ is now stored in variable x.
But say, we have to add a number to a variable and then update the variable with the new value. We can do that too using a modified assignment operator.
Here’s how,
int x = 10;
x += 2;
the above code is the same as,
int x = 10;
x = x + 2;
Here is the list of assignment operators in Java.
Comparison Java Operators
Also known as Relational Operators. Comparison operators will be used to write conditional statements in Java. If the condition is met it returns boolean true and if not, then boolean false.
Let’s see an example code,
public class OperatorsDemo {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
System.out.println("a=10, b=20, c=10");
System.out.println("================");
System.out.println("a > b :"+(a>b));
System.out.println("a < b :"+(a<b));
System.out.println("a = c :"+(a==b));
System.out.println("a >= b :"+(a>=b));
System.out.println("a <= c :"+(a<=c));
System.out.println("a != c :"+(a!=b));
System.out.println("================");
}
}
OUTPUT
a=10, b=20, c=10
================
a > b :false
a < b :true a = c :false a >= b :false
a <= c :true
a != c :true
================
Logical Operators
Now think that if we want to check more than one condition, then what to do? Well, we can use logical operators to modify conditional statements. There are three conditional operators,
- and – Symbol: ‘&&’ – combines two conditional statements and both have to be true for the overall statement to be true. Exactly like literal ‘and’.
- or – Symbol: ‘||’ – Like literal ‘or’, either one of the statements has to be true for the overall condition to be true.
- not – Symbol: ‘ ! ‘ – Same as the word ‘not’, the given conditional statement has to be false for the overall statement to return true.
public class OperatorsDemo {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
System.out.println("a = 10, b = 20, c = 10");
System.out.println("===========================");
System.out.println("10 > 20 and 10 = 10 :"+((a>b)&&(a==c)));
System.out.println("10 > 20 or 10 = 10 :"+((a>b)||(a==c)));
System.out.println("10 < 20 and 10 = 10 :"+((a<b)&&(a==c)));
System.out.println(" not 10 < 20 :"+!(a<20));
System.out.println("============================");
}
}
OUTPUT
a = 10, b = 20, c = 10
===========================
10 > 20 and 10 = 10 :false
10 > 20 or 10 = 10 :true
10 < 20 and 10 = 10 :true
not 10 < 20 :false
============================
List of Java Logical Operators,
Bitwise Java Operators
- & – AND – Sets each bit to 1 if both bits are 1.
For instance, 5 & 1. Same as 0101 & 0001. Will result in 0001 or decimal 1. - | – OR – Sets each bit to 1 if any of the two bits is 1
For instance, 5 | 1. Same as 0101 | 0001, will result in 0101 or decimal 5. - ~ – NOT – Inverts all the bits.
For instance, ~5. Same as ~0101, will result in 1010 or decimal 10. - ^ – XOR – Sets each bit to 1 if only one of the two bits is 1.
For instance, 5 ^ 1. Same as 0101 ^ 0001, will result in 0100 or decimal 4. - << – Zero-fill left shift – Shift left by pushing zeroes in from the right and letting the leftmost bits fall off.
- >> – Signed right shift – Shift right by pushing copies of the leftmost bit in from the left and letting the rightmost bits fall off.
- >>> – Zero-fill right shift – Shift right by pushing zeroes in from the left and letting the rightmost bits fall off.
The leftmost bit is called MSB, the most significant bit, and the rightmost bit is called LSB, the least significant bit.