Operators in Java
Java offers various types of operators to perform the conventional operations like assignment, conditional checks, addition, subtraction, etc.
Types of Operators
- Unary operator
- Arithmetic operator
- Relational operator
- Logical operator
- Bitwise operator
- Assignment operator
- Ternary operator
Java Unary Operator
Java provides the Unary operator and it requires only one operand to perform the operation. There are 2 types of Unary Operators in java such as Prefix and Postfix operators.
Name |
Operator |
Description |
Prefix |
++exp |
Its an Incremental operator and it will increase the current value by 1 before the execution |
--exp |
Its a Decremental operator and it will decrease the current value by 1 before the execution |
Postfix |
exp++ |
Its an Incremental operator and it will increase the current value by 1 after the execution |
exp-- |
Its a Decremental operator and it will decrease the current value by 1 after the execution |
Example: #1 - Prefix operator
Print the value of i, j
and x, y before prefix operation and after prefix operation.
Java Input Screen
class Operators {
public static void main(String[] args) {
// PREFIX
System.out.println("Prefix Increment");
int i = 6;
System.out.println("The value of i before prefix increment is " + i);
int j = ++i; // increment the value by 1 and assign
System.out.println("The value of i before prefix increment is " + i);
System.out.println("The value of j is " + j);
System.out.println("Prefix Decrement");
int x = 3;
System.out.println("The value of x after prefix decrement is " + x);
int y = --x; // decrement the value by 1 and assign
System.out.println("The value of x after prefix decrement is " + x);
System.out.println("The value of y is " + y);
}
}
Java Output Screen
Prefix Increment
The value of i before prefix increment is 6
The value of i after prefix increment is 7
The value of j is 7
Prefix Decrement
The value of x before prefix decrement is 3
The value of x after prefix decrement is 2
The value of y is 2
Example: #2 - Postfix operator
Print the value of i, j
and x, y
before postfix operation and after postfix operation.
Java Input Screen
class Operators {
public static void main(String[] args) {
// POSTFIX
System.out.println("Postfix Increment");
int i = 6;
System.out.println("The value of i before postfix increment is " + i);
int j = i++; // assign and increment the value by 1
System.out.println("The value of i before postfix increment is " + i);
System.out.println("The value of j is " + j);
System.out.println("Postfix Decrement");
int x = 3;
System.out.println("The value of x before postfix decrement is " + x);
int y = x--; // assign and decrement the value by 1
System.out.println("The value of x after postfix decrement is " + x);
System.out.println("The value of y is " + y);
}
}
Java Output Screen
Postfix Increment
The value of i before postfix increment is 6
The value of i after postfix increment is 7
The value of j is 6
Postfix Decrement
The value of x before postfix decrement is 3
The value of x after postfix decrement is 2
The value of y is 3
Thousands Lived without Love, but not without water. So SAVE WATER.
Java Arithmetic Operator
Java provides the Arithmetic operator and it requires two operands to perform the operation. It performs the operations like addition, subtraction, multiplication, division, modulo.
Name |
Operator |
Description |
Addition |
+ |
It adds two numbers i.e. a + b |
subtraction |
- |
It subtracts two numbers i.e. a - b |
Multiplication |
* |
It multiples two numbers i.e. a * b |
Division |
/ |
It divides two numbers i.e. a / b |
Modulo (or) Modulus |
% |
It returns the remainder of the division operation between two numbers i.e. a / b |
Example: #3 - Arithmetic operator
Print the value of a and b with various Arithmetic operation.
Java Input Screen
class Operators {
public static void main(String[] args) {
int x = 5, y = 3;
System.out.println("The value of (x + y) is " + (x + y));
System.out.println("The value of (x - y) is " + (x - y));
System.out.println("The value of (x * y) is " + (x * y));
System.out.println("The value of (x / y) is " + (x / y));
System.out.println("The value of (x % y) is " + (x % y));
}
}
Java Output Screen
The value of (x + y) is 8
The value of (x - y) is 2
The value of (x * y) is 15
The value of (x / y) is 1
The value of (x % y) is 2
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #4 - Arithmetic operator division
Divide a number with different values of data type int and double and print the result respectively.
Java Input Screen
class Operators {
public static void main(String[] args) {
int i = 5, j = 2;
double k = 2.0;
// By default, the data type of the result is int (i.e. 2)
System.out.println("The value of (i / j) is " + (i / j));
// By default, the data type of the result is double (i.e. 2.5)
System.out.println("The value of (i / k) is " + (i / k));
// Cast the divident of type double (i.e. j)
// So the result is 2.5
System.out.println("The value of (i / (double)j) is " + (i / (double)j));
}
}
Java Output Screen
The value of (i / j) is 2
The value of (i / k) is 2.5
The value of (i / (double)j) is 2.5
Thousands Lived without Love, but not without water. So SAVE WATER.
Java Relational Operator
Java provides the Relational operator to compare two operand values of same data type. It performs the operations like
Logical AND &&,
Logical OR ||
and Logical NOT !.
Name |
Operator |
Description |
Equal to |
== |
Compares two values and returns true only if both are equal. |
Not Equal to |
!= |
Compares two values and returns true only if both are not equal. |
Less than |
< |
Compares two values and returns true only if the left hand side operand is less than to the right hand side operand. |
Greater than |
> |
Compares two values and returns true only if the left hand side operand is greater than to the right hand side operand. |
Less than or equal to |
<= |
Compares two values and returns true only if the left hand side operand is less than or equal to the right hand side operand. |
Greater than or equal to |
>= |
Compares two values and returns true only if the left hand side operand is greater than or equal to the right hand side operand. |
Example: #5 - Relational Operator
Example of Relational like Equal to ==, Not Equal to !=,
Less than <, Greater than >, Less than or equal to <=
and Greater than or equal to >=
Java Input Screen
class Operators {
public static void main(String[] args) {
String name = "John";
int age = 24;
// String Comparision
boolean result = name == "John"; // true
System.out.println("Check the name is John : " + result);
// String Comparision with inbuilt inline String method
result = name.equals("John"); // true
System.out.println("Check the name is John using String.equals : " + result);
// Numeric Comparision
result = age == 24; // true
System.out.println("Check the person age is 24 : " + result);
result = age != 20; // true
System.out.println("Check the person age is not equal to 20 : " + result);
result = age < 18; // false
System.out.println("Check the person is minor (i.e. less than 18) : " + result);
result = age > 58; // false
System.out.println("Check the person is senior citizen (i.e. greater than 58) : " + result);
result = age <= 18; // false
System.out.println("Check the person age is less than or equal to 18 : " + result);
result = age >= 24; // true
System.out.println("Check the person age is greater than or equal to 24 : " + result);
}
}
Java Output Screen
Check the name is John : true
Check the name is John using String.equals : true
Check the person age is 24 : true
Check the person age is not equal to 20 : true
Check the person is minor (i.e. less than 18) : false
Check the person is senior citizen (i.e. greater than 58) : false
Check the person age is less than or equal to 18 : false
Check the person age is greater than or equal to 24 : true
Thousands Lived without Love, but not without water. So SAVE WATER.
Java Logical Operator
Java provides the Logical operator and it requires two operands to perform the operation. It performs the operations like Logical AND &&, Logical OR || and Logical NOT !.
Name |
Operator |
Description |
Logical AND |
&& |
Compares two expressions and returns true only if both evaluate to true. |
Logical OR |
|| |
Compares two expressions and returns true either one evaluate to true. |
Logical NOT |
! |
Inverts the boolean value of an expression |
Example: #6 - Logical Operator
Example of Logical AND &&, Logical OR || and Logical NOT !
Java Input Screen
class Operators {
public static void main(String[] args) {
int john = 32, peter = 19;
// Logical AND operation
boolean result1 = (john > 18) && (peter > 18);
boolean result2 = (john < 18) && (peter < 18);
System.out.println("Both are above 18 years (Logical AND) : " + result1);
System.out.println("Both are below 18 years (Logical AND) : " + result2);
// Logical OR operation
boolean result3 = (john > 18) || (peter > 18);
boolean result4 = (john > 30) || (peter > 30);
boolean result5 = (john > 50) || (peter > 50);
System.out.println("Anyone is above 18 years (Logical OR) : " + result3);
System.out.println("Anyone is above 30 years (Logical OR) : " + result4);
System.out.println("Anyone is above 50 years (Logical OR) : " + result5);
// Logical NOT operation
boolean result6 = !(john > 18);
System.out.println("Check the john's age is below 18 years : " + result6);
}
}
Java Output Screen
Both are above 18 years (Logical AND) : true
Both are below 18 years (Logical AND) : false
Anyone is above 18 years (Logical OR) : true
Anyone is above 30 years (Logical OR) : true
Anyone is above 50 years (Logical OR) : false
Check the john's age is below 18 years : false
Java Bitwise Operator
Java provides the Bitwise operator and it requires two operands to perform the operation. It performs the operations like Signed Left Shift <<, Signed Right Shift >> and Unsigned Right Shift >>>.
Name |
Operator |
Description |
Left Shift |
<< |
Converts the numerical representation to the equivalent binary representation and shifts a 0 into the rightmost n position |
Signed Right Shift |
>> |
Converts the numerical representation to the equivalent binary representation and shifts 1 only if the number is negative otherwise 0 into the leftmost n position |
Unsigned Right Shift |
>>> |
Converts the numerical representation to the equivalent binary representation and shifts a 0 into the leftmost n position |
Bitwise AND |
& |
Converts the numerical representation to the equivalent binary representation and perform the logical AND && for each bit against two operands |
Bitwise OR |
| |
Converts the numerical representation to the equivalent binary representation and perform the logical OR || for each bit against two operands |
Bitwise XOR |
^ |
Converts the numerical representation to the equivalent binary representation and perform the logical XOR for each bit against two operands |
Bitwise Complement |
~ |
Converts the numerical representation to the equivalent binary representation and perform the logical NOT ! for each bit. |
Example: #7 - Bitwise Shift Operator
Example of Bitwise operators - Left Shift <<, Signed Right Shift >> and Unsigned Right Shift >>>
Java Input Screen
class Operators {
public static void main(String[] args) {
int x = -6, y = 6, result = 0;
// Left Shift
// Remove the leftmost two binary digits
// and place the binary b'0' to the rightmost 2 position
// to make it 32 bit in length
// x => 11111111 11111111 11111111 11111010 => -6
// x << 2 => 11111111 11111111 11111111 11101000 => -24
// y => 00000000 00000000 00000000 00000110 => 6
// y << 2 => 00000000 00000000 00000000 00011000 => 24
result = x << 2;
System.out.println("Left Shift (x << 2) is : " + result);
result = y << 2;
System.out.println("Left Shift (y << 2) is : " + result);
// Signed Right Shift - Negative Number
// Remove the rightmost two binary digits
// and place the binary b'1' to the leftmost 2 position
// to make it 32 bit in length
// x => 11111111 11111111 11111111 11111010 => -6
// x >> 2 => 11111111 11111111 11111111 11111110 => -2
// Signed Right Shift - Positive Number
// Remove the rightmost two binary digits
// and place the binary b'0' to the leftmost 2 position
// to make it 32 bit in length
// y => 00000000 00000000 00000000 00000110 => 6
// y >> 2 => 00000000 00000000 00000000 00000001 => 1
result = x >> 2;
System.out.println("Signed Right Shift (x >> 2) is : " + result);
result = y >> 2;
System.out.println("Signed Right Shift (y >> 2) is : " + result);
// Unsigned Right Shift
// Remove the rightmost two binary digits
// and place the binary b'0' to the leftmost 2 position
// to make it 32 bit in length
// x => 11111111 11111111 11111111 11111010 => -6
// x >>> 2 => 00111111 11111111 11111111 11111110 => 1073741822
// y => 00000000 00000000 00000000 00000110 => 6
// y >>> 2 => 00000000 00000000 00000000 00000001 => 1
result = x >>> 2;
System.out.println("Unsigned Right Shift (x >>> 2) is : " + result);
result = y >>> 2;
System.out.println("Unsigned Right Shift (y >>> 2) is : " + result);
}
}
Java Output Screen
Left Shift (x << 2) is : -24
Left Shift (y << 2) is : 24
Signed Right Shift (x >> 2) is : -2
Signed Right Shift (y >> 2) is : 1
Unsigned Right Shift (x >>> 2) is : 1073741822
Unsigned Right Shift (y >>> 2) is : 1
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #8 - Bitwise Operator
Example of Bitwise operators - Bitwise AND &, Bitwise OR |, Bitwise XOR ^ and Bitwise Complement ~.
Java Input Screen
class Operators {
public static void main(String[] args) {
int x = 6, y = 3, result = 0;
// Bitwise AND
// x => 00000000 00000000 00000000 00000110 => 6
// y => 00000000 00000000 00000000 00000011 => 3
// x & y => 00000000 00000000 00000000 00000010 => 2
result = x & y;
System.out.println("Bitwise AND (x & y) is : " + result);
// Bitwise OR
// x => 00000000 00000000 00000000 00000110 => 6
// y => 00000000 00000000 00000000 00000011 => 3
// x | y => 00000000 00000000 00000000 00000111 => 7
result = x | y;
System.out.println("Bitwise OR (x | y) is : " + result);
// Bitwise XOR
// x => 00000000 00000000 00000000 00000110 => 6
// y => 00000000 00000000 00000000 00000011 => 3
// x ^ y => 00000000 00000000 00000000 00000101 => 5
result = x ^ y;
System.out.println("Bitwise XOR (x ^ y) is : " + result);
// Bitwise Complement
// x => 00000000 00000000 00000000 00000110 => 6
// ~x => 11111111 11111111 11111111 11111001 => -7
result = ~x;
System.out.println("Bitwise Complement ~x is : " + result);
}
}
Java Output Screen
Bitwise AND (x & y) is : 2
Bitwise OR (x | y) is : 7
Bitwise XOR (x ^ y) is : 5
Bitwise Complement ~x is : -7
Thousands Lived without Love, but not without water. So SAVE WATER.
Java Assignment Operator
Java provides the Assignment operator and it requires two operands to perform the operation. It performs the operations like addition, subtraction, multiplication, division, modulo.
Name |
Operator |
Description |
Simple Assignment |
= |
Assigns a value to a variable. |
Increment Assignment |
+= |
Adds a value and the variable and assigns the result to that variable. |
Decrement Assignment |
-= |
Subtracts a value and the variable and assigns the result to that variable. |
Multiplication Assignment |
*= |
Multiplies a value and the variable and assigns the result to that variable. |
Division Assignment |
/= |
Divides a value and the variable and assigns the result to that variable. |
Modulo Assignment / Modulus assignment |
%= |
Modulo a value and the variable and assigns the result to that variable. |
Bitwise Left shift assignment |
<<= |
Performs a Bitwise Left shift operation between a variable and a value, finally assigns the result to that variable. |
Bitwise Singed Right shift assignment |
>>= |
Performs a Bitwise Singed Right shift operation between a variable and a value, finally assigns the result to that variable. |
Bitwise Unsigned Right shift assignment |
>>>= |
Performs a Bitwise Unsigned Right shift operation between a variable and a value, finally assigns the result to that variable. |
Bitwise AND assignment |
&= |
Performs a Bitwise AND operation between a variable and a value, finally assigns the result to that variable. |
Bitwise OR assignment |
|= |
Performs a Bitwise OR operation between a variable and a value, finally assigns the result to that variable. |
Bitwise XOR assignment |
^= |
Performs a Bitwise XOR operation between a variable and a value, finally assigns the result to that variable. |
Example: #9 - Arithmetic Assignment operator
Print the resultant value of x, y with various Arithmetic Assignment operation.
Java Input Screen
class Operators {
public static void main(String[] args) {
int x = 6, y = 1;
y += x; // 6 + 1 = 7
System.out.println("The value of y => (y += x) is " + y);
y = 10;
y -= x; // 10 - 6 = 4
System.out.println("The value of y => (y -= x) is " + y);
y = 2;
y *= x; // 6 * 2 = 12
System.out.println("The value of y => (y *= x) is " + y);
y = 18;
y /= x; // 18 / 6 = 3
System.out.println("The value of y => (y /= x) is " + y);
y = 20;
y %= x; // 20 % 6 = 2
System.out.println("The value of y => (y %= x) is " + y);
}
}
Java Output Screen
The value of y => (y += x) is 7
The value of y => (y -= x) is 4
The value of y => (y *= x) is 12
The value of y => (y /= x) is 3
The value of y => (y %= x) is 2
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #10 - Bitwise Assignment operator
Print the resultant value of x, y with various Bitwise Assignment operation.
Java Input Screen
class Operators {
public static void main(String[] args) {
int x = 2, y = 6;
y <<= x; // 6 << 2 = 24
System.out.println("The value of y => (y <<= x) is " + y);
x = 2;
y = 6;
y >>= x; // 6 >> 2 = 1
System.out.println("The value of y => (y >>= x) is " + y);
x = 2;
y = 6;
y >>>= x; // 6 >>> 2 = 1
System.out.println("The value of y => (y >>>= x) is " + y);
x = 3;
y = 6;
y &= x; // 6 & 3 = 2
System.out.println("The value of y => (y & x) is " + y);
x = 3;
y = 6;
y |= x; // 6 | 3 = 7
System.out.println("The value of y => (y | x) is " + y);
x = 3;
y = 6;
y ^= x; // 6 ^ 3 = 5
System.out.println("The value of y => (y ^ x) is " + y);
}
}
Java Output Screen
The value of y => (y <<= x) is 24
The value of y => (y >>= x) is 1
The value of y => (y >>>= x) is 1
The value of y => (y &= x) is 2
The value of y => (y |= x) is 7
The value of y => (y ^= x) is 5
BBMINFO