Java Operators
An operator is used to perform an operation over one or more operands. It is a character that represents an action, for example * is an arithmatic operator that represents multiplication.
Operator in Java are classified as:-
- Assignment Operator
- Arithmetic Operator
- Auto-increment and Auto-decrement Operator
- Logical Operator
- Relation Operator
- Bitwise Operator
- Ternary Operator
1) Assignment Operator:
Assignments operators in java are: =, +=, -=, *=, /=, %=
Operator | Description |
---|---|
= | It is used to assign the value on the right hand side to the operand on the left. Example: int a=6; The value 6 is assigned to the variable ‘a’ |
+= | Performs addition & assigns the result to the left hand side. Example: a+=6; //(a=a+6) |
-= | Performs subtraction and assigns the result to the right hand side. Example: a-=6; //(a = a-6) |
*= | Performs multiplication and assigns the result to the right hand side. Example: a*=6; //(a = a*6) |
/= | Performs division and assigns the result to the right hand side. Example: a/=6; //(a = a/6) |
%= | Performs division and assigns the remainder to the right hand side. Example: a%=6; //(a = a%6) |
Example:
public class AssignmentOperatorDemo {
public static void main(String args[]) {
int a = 10;
int b = 20;
b = a;
System.out.println("= Output: "+b);
b += a;
System.out.println("+= Output: "+b);
b -= a;
System.out.println("-= Output: "+b);
b *= a;
System.out.println("*= Output: "+b);
b /= a;
System.out.println("/= Output: "+b);
b %= a;
System.out.println("%= Output: "+a);
}
}
Output
= Output: 10
+= Output: 20
-= Output: 10
*= Output: 100
/= Output: 10
%= Output: 0
2) Arithmetic Operator:
Arithmetic Operator are used to perform arithmetic operations over two operands. Basic arithmetic operators are: +, -, *, /, %
Operator | Description |
---|---|
+ | Adds both of the operands and returns the result. Example: int a = 10, b=2, c; c = a + b; //c = 12 |
– | Subtracts the right hand side operand from the left hand side operand. Example: c = a – b; //c = 2 |
* | Multiplies both the operands and returns the result. Example: c = a * b; //c = 20 |
/ | Divides left hand side operand by the right hand side operand. Example: c = a / b; //c =5 |
% | Divides left hand side operand by the right hand side operand and returns the reminder. Example: c = a % b; //c = 0 |
Example:
public class ArithmeticOperatorDemo {
public static void main(String args[]) {
int a = 80;
int b = 10;
System.out.println("a + b: " + (a + b) );
System.out.println("a - b: " + (a - b) );
System.out.println("a * b: " + (a * b) );
System.out.println("a / b: " + (a / b) );
System.out.println("a % b: " + (a % b) );
}
}
Output:
a + b: 90
a – b: 70
a * b: 800
a / b: 8
a % b: 0
3) Auto-increment and Auto-decrement Operators:
Auto-increment operator is: ++ and auto-decrement operator is: –.
a++ is equivalent to a=a+1.
a– is equivalent to a=a-1.
Example:
public class AutoOperatorDemo {
public static void main(String args[]){
int a=10;
int b=20;
a++;
b--;
System.out.println("a++ is: "+a);
System.out.println("b-- is: "+b);
}
}
Output:
a++ is: 11
b– is: 19
4) Logical Operators:
Logical Operators are used with binary variables. They are mainly used in conditional statements and loops for evaluating a condition.
Logical operators in java are:
- Logical AND (&&),
- Logical OR (||),
- Logical NOT (!)
Operator | Description |
---|---|
Logical AND | If both the operands are true, then the condition evaluates to true. Otherwise as false. Consider if X = true, Y = true, then result is true. |
Logical OR | If any of the operand is true, then the condition evaluates to true. Otherwise as false. Consider if X = true, Y = true, then result is true. |
Logical NOT | It would result the opposite result i.e. if X = true then !X = False |
Example:
public class LogicalOperatorDemo {
public static void main(String args[]) {
boolean X = true;
boolean Y = false;
System.out.println("X && Y: " + (X&&Y));
System.out.println("X || Y: " + (X||Y));
System.out.println("!(X && Y): " + !(X&&Y));
}
}
Output:
X && Y: false
X || Y: true
!(X && Y): true
5) Relational Operator:
Relational operators are used to compare the values of two operands. We have six relational operators in Java: ==, !=, >, <, >=, <=.
Operator | Description |
---|---|
== | Checks whether values of both side of the operands are equal. |
!= | Checks whether values of both the operands are unequal. Example: check = (a! = b); // true if ‘a’ is not equal to ‘b’ |
> | Checks whether the operand in the left hand side is greater than the right hand side operator. Example: int a = 5, b = 2; (a > b) would return true. |
< | Checks whether the operand in the left hand side is less than the right hand side operator. Example: int a = 5, b = 2; (a < b) would return false. |
>= | Checks whether the operand in the left hand side is greater than or equal to the right hand side operator. Example: int a = 2, b = 2; (a >= b) would return true. |
<= | Checks whether the operand in the left hand side is less than or equal to the right hand side operator. Example: int a = 3, b = 2; (a <= b) would return false. |
NOTE: This example is using if-else statement which is our next tutorial, if you are finding it difficult to understand then refer if-else in Java.
Example:
public class RelationalOperatorDemo {
public static void main(String args[]) {
int a = 10;
int b = 50;
if (a==b) {
System.out.println("a and b are equal");
}
else{
System.out.println("a and b are not equal");
}
if( a != b ){
System.out.println("a and b are not equal");
}
else{
System.out.println("a and b are equal");
}
if( a > b){
System.out.println("a is greater than b");
}
else{
System.out.println("a is not greater than b");
}
if( a >= b ){
System.out.println("a is greater than or equal to b");
}
else{
System.out.println("a is less than b");
}
if( a < b ){
System.out.println("a is less than b");
}
else{
System.out.println("a is not less than b");
}
if( a <= b){
System.out.println("a is less than or equal to b");
}
else{
System.out.println("a is greater than b");
}
}
}
Output:
a and b are not equal
a and b are not equal
a is not greater than b
a is less than b
a is less than b
a is less than or equal to b
6) Bitwise Operator:
There are six bitwise Operators:
- Bitwise complement (~)
- Bitwise AND (&)
- Bitwise inclusive OR (|)
- Bitwise exclusive OR (^)
- Left shift (<<)
- Right shift (>>)
Operator | Description |
---|---|
Bitwise complement | This operator inverts a bit pattern making 0’s to 1 and 1’s to 0. Example: 00001100 would be change to 11110011 |
Bitwise AND | Performs bitwise AND operation. Example: consider x = 15, y = 6, (x = 0000 1111, y = 0000 0110), x&y = 6 (0000 0110) |
Bitwise inclusive OR | Performs bitwise OR operation. Example: consider x = 14, y = 12 (x = 0000 1110, y = 0000 1100), result is: 14 (0000 1110) |
Bitwise exclusive OR | Performs bitwise XOR operation. Example: consider x = 15, y = 12 (x = 0000 1111, y = 0000 1100) x^y = 3 (0000 0011) |
Left shift | The operand’s value is moved left by the number of bits specified in the right side of the operator <<. Example: consider x = 7 (0000 0111), x<<2 = 24 (0001 1100) |
Right shift | The operand’s value is moved right by the number of bits specified in the right side of the operator >>. Example: consider x = 7 (0000 0111), x>>2 = 1 (0001) |
Example:
public class BitwiseOperatorDemo {
public static void main(String args[]) {
int a = 11; /* 11 = 00001011 */
int b = 22; /* 22 = 00010110 */
int result = 0;
result = a & b;
System.out.println("a & b: "+result);
result = a | b;
System.out.println("a | b: "+result);
result = a ^ b;
System.out.println("a ^ b: "+result);
result = ~a;
System.out.println("~a: "+result);
result = a << 2;
System.out.println("a << 2: "+result);
result = a >> 2;
System.out.println("a >> 2: "+result);
}
}
Output
a & b: 2
a | b: 31
a ^ b: 29
~a: -12
a << 2: 44
a >> 2: 2
7) Ternary Operator:
This operator evaluates a boolean expression and assign the value based on the result.
Syntax:
variable num1 = (expression) ? value if true : value if false
If the expression results true then the first value before the colon (:) is assigned to the variable num1 else the second value is assigned to the num1.
Example:
public class TernaryOperatorDemo {
public static void main(String args[]) {
int num1, num2;
num1 = 25;
/* num1 is not equal to 10 that's why
* the second value after colon is assigned
* to the variable num2
*/
num2 = (num1 == 10) ? 100: 200;
System.out.println( "num2: "+num2);
/* num1 is equal to 25 that's why
* the first value is assigned
* to the variable num2
*/
num2 = (num1 == 25) ? 100: 200;
System.out.println( "num2: "+num2);
}
}
Output:
num2: 200
num2: 100