Java - Operators
Operators are used in Java to perform calculations or operations.
Say for example, we have used the addition + operator to add two numbers.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 6;
int z = x + y;
System.out.println("The added value is : "+z);
}
}
Output :
The added value is : 11
In the above example, we have used two operators of Java.
-
Addition Operator (i.e. +)
Used to add two or more values.
-
Assignment Operator (i.e. =)
Used to assign a value to a variable.
int x = 5;
int y = 6;
int z = x + y;
So far, we have seen only two operators. Now, let us see all the operators available in Java.
List of Operators available in Java
-
Arithmetic Operators
Arithmetic Operators are used to perform Arithmetic operations like addition, subtraction, multiplication e.t.c.
Let us see them below.
Operator |
Used For |
Example |
+ |
Addition |
x + y |
- |
Subtraction |
x - y |
* |
Multiplication |
x * y |
/ |
Division |
x / y |
% |
Modulus |
x % y |
++ |
Increment |
x++ |
-- |
Decrement |
x-- |
Addition Operator (+)
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 6;
int z = x + y;
System.out.println("The added value is : "+z);
}
}
Output :
The added value is : 11
Subtraction Operator (-)
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 8;
int y = 2;
int z = x - y;
System.out.println("The subtracted value is : "+z);
}
}
Output :
The subtracted value is : 6
Multiplication Operator (*)
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 6;
int z = x * y;
System.out.println("The multiplied value is : "+z);
}
}
Output :
The multiplied value is : 30
Division Operator (/)
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 8;
int y = 2;
int z = x / y;
System.out.println("The divided value is : "+z);
}
}
Output :
The divided value is : 4
Modulus Operator (%)
Modulus Operator calculates the remainder.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 9;
int y = 2;
int z = x % y;
System.out.println("The modulus is : "+z);
}
}
Output :
The modulus is : 1
i.e. The remainder of 9 and 2 is 1.
Increment Operator (++)
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 2;
x++;
System.out.println("The incremented value is : "+x);
}
}
Output :
The incremented value is : 3
The increment operator ++ with x,
Actually means, increment the value of x by 1.
In other words,
Is,
So, the value of x is 2. And it gets incremented by 1.
And the current value of x is 3.
Decrement Operator (--)
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 2;
x--;
System.out.println("The decremented value is : "+x);
}
}
Output :
The decremented value is : 1
The decrement operator -- with x,
Actually means, decrement the value of x by 1.
In other words,
Is,
So, the value of x is 2. And it gets incremented by 1.
And the current value of x is 1.
-
Bitwise Operators
The are a few number system. Among which we usually use the decimal numbers (i.e. 1, 2, 3, .... ). And there is also something called Binary Numbers(i.e. 0 and 1).
Say for example, if we convert the decimal number 5 to Binary, it is 101.
0 and 1 are also called as Bit. And the operator that we are going to use, deals with Bits.
So, they are called as Bitwise Operators.
Let us see them below :
Operator |
Name |
Example |
& |
AND |
x & y |
|| |
OR |
x || y |
^ |
XOR |
x ^ y |
>> |
Right Shift |
x >> |
<< |
Left Shift |
x << |
The Operator & named AND
The and Operator checks, if both the bits are 1 then it returns 1 else it returns 0.
Let us understand it with the below example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 2;
int z = x & y;
System.out.println(z);
}
}
Output :
0
So, in the above code, we have,
And,
So, what Java does internally is, converts both the numbers (i.e. 5 and 2) to binary.
The binary equivalent of 5 is 101 And the binary equivalent of 2 is 10
Now, if we calculate the & of 5 and 2.
And we got the output 000 because we could only get a 1 if both are 1.
But in this case, 1 & 0 is 0, 0 & 1 is 0 and 1 & 0 is also 0 i.e. 000.
And if we convert 000 to decimal we get 0 as output.
The Operator | named OR
The OR Operator checks, if either one of the bits is 1 then it returns 1 else it returns 0.
Let us understand it with the below example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 2;
int z = x | y;
System.out.println(z);
}
}
Output :
7
So, in the above code, we have,
And,
So, what Java does internally is, converts both the numbers (i.e. 5 and 2) to binary.
The binary equivalent of 5 is 101 And the binary equivalent of 2 is 10
Now, if we calculate the OR of 5 and 2.
And we got the output 111 because we can get a 1 if either of the bits are 1.
But in this case, 1 or 0 is 1, 0 or 1 is 1 and 1 or 0 is also 1 i.e. 111.
And if we convert 111 to decimal, it is 7.
The Operator ^ named XOR
The ^ operator named XOR/eXclusive OR is almost same as the OR Operator. The only difference is, if both are 1 it returns 0. Else for 1 and 0 or 0 and 1 it returns 1.
Let us understand it with the below example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 4;
int z = x ^ y;
System.out.println(z);
}
}
Output :
1
So, in the above code, we have,
And,
So, what Java does internally is, converts both the numbers (i.e. 5 and 2) to binary.
The binary equivalent of 5 is 101 And the binary equivalent of 2 is 100
Now, if we calculate the xor of 5 and 2.
And we got the output 001 because 1 xor 1 is 0, 0 xor 0 is 0 and 1 xor 0 is 1
i.e. 001.
And if we convert 001 to decimal, it is 1.
Just note that 1 xor 1 is 0 and not 1.
The Operator >> or Right Shift
The >> Operator or Right Shift, simply shifts bits to the right by number of positions specified.
Let us understand it with the below example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int z = x >> 1;
System.out.println(z);
}
}
Output :
2
So, in the above code, we have,
And we try Right Shifting the value of x by 1.
So, what Java does internally is, converts the number (i.e. 5) to binary.
The binary equivalent of 5 is 101
Now, if we right shift 101 by 1.
And after Right Shifting by 1, the 1 to the extreme right is lost.
And we got the output 010.
And if we convert 010 to decimal, it is 2.
The Operator << or Left Shift
The << Operator or Left Shift, simply shifts bits to the left by number of positions specified.
Let us understand it with the below example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int z = x << 2;
System.out.println(z);
}
}
Output :
20
So, in the above code, we have,
And we try Left Shifting the value of x by 2.
So, what Java does internally is, converts the number (i.e. 5) to binary.
The binary equivalent of 5 is 101
Now, if we left shift 101 by 2.
And after Left Shifting by 2, all the elements shifts 2 places to the right.
And we got the output 10100.
And if we convert 10100 to decimal, it is 20.
-
Assignment Operators
We have already used the Assignment Operator i.e. =. When we tried to initialise any variable.
Say for example,
Although, there is just one assignment operator i.e. =. But there are various ways by which we can write it.
Let us see them below.
Operator |
Example |
Is Same As |
= |
x = 1 |
x = 1 |
+= |
x += 1 |
x = x + 1 |
-= |
x -= 1 |
x = x - 1 |
*= |
x *= 1 |
x = x * 1 |
/= |
x /= 1 |
x = x / 1 |
%= |
x %= 1 |
x = x % 1 |
The Operator =
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
System.out.println("The value of x is : "+x);
}
}
Output :
The value of x is : 5
The operator +=
Say, we have a statement,
Now, we can shorten the above statement by using the operator +=.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
x += 3;
System.out.println("The new value of x is : "+x);
}
}
Output :
The new value of x is : 8
The operator -=
Say, we have a statement,
Now, we can shorten the above statement by using the operator -=.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int x -=3;
System.out.println("The value of x is : "+x);
}
}
Output :
The new value of x is : 2
The operator *=
Say, we have a statement,
Now, we can shorten the above statement by using the operator *=.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
x *= 3;
System.out.println("The value of x is : "+x);
}
}
Output :
The new value of x is : 15
The operator /=
Say, we have a statement,
Now, we can shorten the above statement by using the operator /=.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
x /= 3;
System.out.println("The value of x is : "+x);
}
}
Output :
The new value of x is : 4
The operator %=
Say, we have a statement,
Now, we can shorten the above statement by using the operator %=.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
x %= 3;
System.out.println("The value of x is : "+x);
}
}
Output :
The new value of x is : 2
-
Comparison Operators
There would be scenarios, where we would have to compare two values.
Say, we wanted to check, if the first number is equal to the other number or not. Or if one number is greater than or less than the other number and so on.
Usually, we would be using the Comparison Operators with if statements and Loops. Which we would be learning in later tutorials.
For now we can compare the values and just print the results. The result would be True if the condition matches. Else it would return False.
There are various comparison operators we have listed below.
Operator |
Name |
Example |
== |
Equal To |
x == y |
!= |
Not Equal |
x != y |
> |
Greater Than |
x > y |
< |
Less Than |
x < y |
>= |
Greater Than or Equal To |
x >= y |
<= |
Greater Than or Equal To |
x <= y |
The Operator ==
It is used to compare two values and check if they are equal or not.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 7;
System.out.println(x == y);
}
}
Output :
false
Since, the values of x and y are not equal. We got the output as false.
Let us see the next Example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 5;
System.out.println(x == y);
}
}
Output :
true
Now, the values of x and y are equal. We got the output as true.
The Operator !=
It is used to compare two values and check if they are not equal. ! is a sign for not.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 7;
System.out.println(x != y);
}
}
Output :
true
Since, the values of x and y are not equal. We got the output as true.
Let us see the next Example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 5;
System.out.println(x != y);
}
}
Output :
false
Now, the values of x and y are equal. We got the output as false.
The Operator >
It is used to compare two values and check if the first value is greater than the second or not.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 7;
System.out.println(x > y);
}
}
Output :
false
Since, the values of x is not greater than y. We got the output as false.
Let us see the next Example.
Example
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 7;
int y = 5;
System.out.println(x > y);
}
}
Output :
true
Now, the values of x is greater than y. We got the output as true.
The Operator <
It is used to compare two values and check if the first value is less than the second or not.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 7;
System.out.println(x < y);
}
}
Output :
true
Since, the values of x is less than y. We got the output as true.
Let us see the next Example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 7;
int y = 5;
System.out.println(x < y);
}
}
Output :
false
Now, the values of x is not less than y. We got the output as false.
The Operator >=
It is used to compare two values and check if the first value is greater or equal to the second or not.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 7;
System.out.println(x >= y);
}
}
Output :
false
Since, the values of x is not greater than or equal y. We got the output as false.
Let us see the next Example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 5;
System.out.println(x >= y);
}
}
Output :
true
Now, the values of x is equal than y. We got the output as true.
The Operator <=
It is used to compare two values and check if the first value is less than or equal to the second or not.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 5;
int y = 7;
System.out.println(x <= y);
}
}
Output :
true
Since, the values of x is less than y. We got the output as true.
Let us see the next Example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int x = 7;
int y = 5;
System.out.println(x <= y);
}
}
Output :
false
Now, the values of x is not less than y. We got the output as false.
-
Logical Operators
There can be scenarios where we need to compare more than two values.
Say for example,
There are four numbers and we want to check if the first number is greater than the second. And also need to check if the third number is greater than the fourth number.
Something like the below one,
firstNumber > secondNumber and thirdNumber > fourth_number
And if you see the above statement, we need an and statement to compare four values.
And thus, Logical Operators comes into picture. It can be and, or, not.
Let's see them below :
Operator |
Description |
Example |
&& |
It is true when both conditions are True. |
i < j && k < l |
|| |
It is true when one of the condition is True |
i < j || k < l |
! |
As the name suggests, it calculates the inverse |
!(i < j || k < l) |
The Operator &&
It is true when both conditions are true.
Let us clear with the below example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int i = 5;
int j = 7;
int k = 8;
int l = 9;
System.out.println(i < j && k < l);
}
}
Output :
True
The above statement checks if i < j and k < l.
Now, since 5 is less than 7 and also 8 is less than 9. The print statement returns true.
System.out.println(i < j && k < l);
Let us see another example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int i = 5;
int j = 7;
int k = 8;
int l = 4;
System.out.println(i < j && k < l);
}
}
Output :
False
So, the above statement checks if i < j and k < l.
Now, since 5 is less than 7 but 8 is not less than 4. The print statement returns False.
System.out.println(i < j && k < l);
Just remember, for and operator, both the conditions needs to be satisfied.
The Operator ||
It is true when one of the condition is true.
Let us clear with the below example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int i = 5;
int j = 7;
int k = 8;
int l = 4;
System.out.println(i < j || k < l);
}
}
Output :
True
So, the above statement checks if i < j or k < l.
Now, since 5 is less than 7 and although 8 is not less than 4. The print statement returns True.
System.out.println(i < j || k < l);
Just remember, for or operator, if only one condition works then the entire condition becomes true.
The Operator !
It is true when a condition is false. As the name suggests, it calculates the inverse.
Let us clear with the below example.
Example :
public class MyApplication
{
public static void main(String[] args)
{
int i = 5;
int j = 7;
int k = 8;
int l = 4;
System.out.println(!(i < j || k < l));
}
}
Output :
False
So, the above statement checks if i < j or k < l.
Now, since 5 is less than 7 and although 8 is not less than 4. The condition is True. And since we are using not operator, False is printed.
System.out.println(!(i < j || k < l));