Basic Operators in Java
Operators are special symbols like +, -, *, /, %, etc, which are used to perform operations on operands (values) in Java.
Java provides various types of operators to perform tasks like arithmetic operations, comparison, logical operations, assignment, and more. Here are some commonly used list of operators in Java:
- Arithmetic Operators
- Assignment Operators
- Logical Operators
- Relational Operators
- Ternary Operator
- Unary Operator
- Bitwise Operators
Arithmetic Operators
Arithmetic operators are used to perform calculations, such as addition, subtraction, multiplication, and division.
List of Arithmetic operators in Java:
Operator | Name | Description | Example |
+ | Addition | Adds two operands. | 10 + 15 |
- | Substraction | Subtracts the second operand from the first operand. | 20 - 5 |
* | Multiplication | Multiplies two operands. | 12 * 10 |
/ | Division | Divides the first operand by the second operand. | 74 / 4 |
% | Modulus | Computes the remainder of the division between two operands. | 90 % 5 |
Example:
public class Sample { public static void main(String[] args) { double x = 50; double y = 2; double additionResult = x + y; double substractionResult = x - y; double multiplicationResult = x * y; double divisionResult = x / y; double modulusResult = x % y; System.out.println(additionResult); System.out.println(substractionResult); System.out.println(multiplicationResult); System.out.println(divisionResult); System.out.println(modulusResult); } }
Output:
52.0 48.0 100.0 25.0 0.0
Assignment Operators
Assignment operators are used to assign a value to a variable.
List of Assignment Operators in Java:
Operator | Description | Example |
= | Assigns a value to a variable. | a = 10 |
+= | Adds right operand with the left operand and assigns result to the left operand. | a += b |
-= | Substracts the right operand from the left operand and assigns the result to the left operand. | a -= b |
*= | Multiplies the operand on the right with the operand on the left and assigns the result to the left operand. | a *= b |
/= | Divides the left operand by the right operand and assigns the result to the left operand. | a /= b |
%= | Modulus and assign operator. It calulates the remainder out of two operand and assign the result to the left operand. | a %= b |
&= | Bitwise AND assignment operator. | a &= b |
^= | Bitwise exclusive OR and assignment operator. | a ^= b |
|= | Bitwise inclusive OR and assignment operator. | a |= b |
<<= | Left shift and assignment operator. | a <<= b |
>>= | Right shift and assignment operator. | a >>= b |
>>>= | Shift right zero fill and assignment operator. | a >>>= b |
Logical Operators
Logical operators are used to determine the logic between variables(values).
List of Logical Operators in Java:
Operator | Name | Description | Example |
&& | Logical AND | Returns true, if and only if both statements are true. | 175 > 156 && 253 > 130 |
|| | Logical OR | Returns true, if one of the given statements is true. | 147 < 112 || 350> 330 |
! | Logical NOT | Change the result to the opposite, returns true if the result is false. | !(2 > 5) |
public class Sample { public static void main(String[] args) { int x = 10; int y = 12; int z = 30; boolean result1 = x < y && y < z; boolean result2 = x > y || y < z; boolean result3 = !(x < y); System.out.println(result1); System.out.println(result2); System.out.println(result3); } }
Output:
true true false
Relational Operators
Relational operators returns a boolean value (true/false) by comparing two variables or numbers.
List of Relational Operators in Java:
Operator | Name | Description | Example |
== | Equal | Compares and return true if the left operand is equal to the right operand. | a == 10 |
!= | Not equal | Compares and return true if the left operand is not equal to the right operand. | a != 10 |
> | Greater than | Compares and return true if the left operand is greater than the right operand. | a > 10 |
< | Less than | Compares and return true if the left operand is lesser than the right operand. | a < 10 |
>= | Greater than or equal to | Compares and return true if the left operand is greater than or equal to the right operand. | a >= 10 |
<= | Less than or equal to | Compares and return true if the left operand is lesser than or equal to the right operand. | a <= 10 |
public class Sample { public static void main(String[] args) { int a = 10; int b = 12; boolean result1 = a == b; boolean result2 = a != b; boolean result3 = a > b; boolean result4 = a < b; boolean result5 = a <= b; boolean result6 = a >= b; System.out.println(result1); System.out.println(result2); System.out.println(result3); System.out.println(result4); System.out.println(result5); System.out.println(result6); } }
Output:
false true false true true false
Ternary Operator
A ternary operator takes three arguments (or operands) to decide, which value should be assigned to the variable. A ternary operator is also called conditional operator. It works as an inline if-else statement.
Syntax of Ternary Operator:
(if this condition is true ? use this : else use this)
Example:
public class Sample { public static void main(String[] args) { int a = 10; int b = 12; int c = 30; int result1 = (a > b ? 1 : 2); int result2 = (a < b ? 1 : 2); boolean result3 = (a > c ? false : true); System.out.println(result1); System.out.println(result2); System.out.println(result3); } }
Output:
2 1 true
Unary Operators
In Java, the unary operators are used to increase or decrease the value of an operand by 1 or a given number. The unary operators have two forms: Postfix and Prefix. Both do increment or decrement in appropriate variables.
List of Unary Operators in Java:
Operator | Name | Description | Example |
i++ | Postfix Increment | Assigns the value of "i" in "a" and then increments the operand i by 1. | int a = i++; |
++i | Prefix Increment | Increments the value of operand "i" by 1 and then assigns the value of "i" to "a". | int a = ++i; |
i-- | Postfix Decrement | Assigns the value of "i" in "a" and then decreases the operand i by 1. | int a = i--; |
--i | Prefix Decrement | Decreases the value of operand "i" by 1 and then assigns the value of "i" to "a". | int a = --i; |
Example:
public class Sample { public static void main(String[] args) { int a = 20; int b = 15; ++a; --b; System.out.println(a); System.out.println(b); } }
Output:
21 14
Bitwise Operators
Bitwise operators in Java are used to perform operations at the bit level, manipulating individual bits of integer types, long, int, short, byte and char values. Bitwise operations are generally faster than division, multiplication, and sometimes faster than addition.
List of Bitwise Operators in Java:
Operator | Symbol | Description |
Bitwise AND | & | Performs a bitwise AND operation between two operands. This operator returns 1 if both of the bits are 1, else it gives 0. |
Bitwise OR | | | Performs a bitwise OR operation between two operands. This operator returns 1 if either of the bits is 1, else it gives 0. |
Bitwise XOR | ^ | Performs a bitwise XOR (exclusive OR) operation between two operands. It sets the bits to 1 where the corresponding bits are different else it sets the bits to 0. |
Bitwise NOT | ~ | Flips the bits of an operand. It is used to invert the bits of a numeric value. It flips each bit from 0 to 1 and from 1 to 0. |
Left Shift | << | Shifts the bits of the left operand to the left by a specified number of positions. |
Right Shift | >> | Shifts the bits of the left operand to the right by a specified number of positions. |
Unsigned Right Shift | >>> | Shifts the bits of the left operand to the right by a specified number of positions, filling the leftmost positions with zeros. |
Bitwise AND Operator Example
In this example, we have two variables x and y, both of type int. x is initialized with the decimal value 4, which in binary representation is 0100. y is initialized with the decimal value 7, which in binary representation is 0111. When we apply the bitwise AND operator (&) between x and y, the operation is performed at the bit level.
public class Example { public static void main(String[] args) { int x = 4; // binary: 0100 int y = 7; // binary: 0111 int result = x & y; System.out.println(result); } }
Output:
4
The resulting value is 4, which is the decimal representation of the binary number 0111.
Here's how the bitwise AND operation works for this example:
x: 0 1 0 0 (4) y: 0 1 1 1 (7) _______________________ result: 0 1 0 0 (4)
This AND (&) operator sets the bit to1 if both of the bits are 1, else it sets to 0. Therefore, the resulting value is 4. which is the decimal representation of the binary number 0100.
Bitwise OR Operator Example
In the given example, we have two variables x and y, both of type int. x is initialized with the decimal value 4, which in binary representation is 0100. y is initialized with the decimal value 7, which in binary representation is 0111.
public class Example { public static void main(String[] args) { int x = 4; // binary: 0100 int y = 7; // binary: 0111 int result = x | y; System.out.println(result); } }
Output:
7
The resulting value is 7, which is the decimal representation of the binary number 0111.
Here's how the bitwise OR operation works for this example:
x: 0 1 0 0 (4) y: 0 1 1 1 (7) _______________________ result: 0 1 1 1 (7)
This Bitwise OR operator sets the bits to 1 if either of the bits is 1, else it sets to 0. Therefore, the resulting value is 7, which is the decimal representation of the binary number 0111.
Bitwise XOR Operator Example
In the given example, we have two variables x and y, both of type int. x is initialized with the decimal value 4, which in binary representation is 0100. y is initialized with the decimal value 7, which in binary representation is 0111.
public class Example { public static void main(String[] args) { int num1 = 4; // binary: 0100 int num2 = 7; // binary: 0111 int result = num1 ^ num2; System.out.println(result); } }
Output:
3
The resulting value is 3, which is the decimal representation of the binary number 0011.
Here's how the bitwise XOR operation works for this example:
num1: 0 1 0 0 (4) num2: 0 1 1 1 (7) _______________________ result: 0 0 1 1 (3)
The bitwise XOR operation sets the bits to 1 if corresponding bits are different, else it sets to 0. Therefore, the resulting value is 3, which is the decimal representation of the binary number 0011.
Bitwise NOT Operator Example
In this example, we have a variable x of type int initialized with the decimal value 8, which in binary representation is 1000.
public class Example { public static void main(String[] args) { int x = 8; // binary: 1000 int result = ~x; System.out.println(result); } }
Output:
-9
The resulting value is -9, which is the decimal representation of the binary number 0111. Since num1 is positive, the bitwise complement operation inverts each bit, including the sign bit. Therefore, the resulting value is negative.
Here's how the bitwise complement operation works for this example:
x: 1 0 0 0 (8) result: 0 1 1 1 (-9)
The Bitwise NOT (~) operator inverts each bit from 0 to 1 and from 1 to 0. The resulting value is -9, which is the decimal representation of the binary number 0111.
Bitwise Left Shift Operator Example
In the example above, we have a variable x initialized with the decimal value 5. In binary representation, it is 0000 0101. The code then applies the left shift operator (<<) to x with a shift value of 2. The left shift operation moves the bits of the number to the left by the specified number of positions.
public class Example { public static void main(String[] args) { int x = 5; // binary: 0000 0101 int shifted = x << 2; System.out.println(shifted); // output: 20 } }
20
The resulting value is 20, which is the decimal representation of the binary number 0001 0100.
Here's how the left shift operation works for this example:
x: 0000 0101 (5) shifted: 0001 0100 (20)
The bits of num are shifted two positions to the left, and the vacant positions on the right are filled with zeros. Therefore, the result value is 20, which is the decimal representation of the binary number 0001 0100.
Bitwise Right Shift Operator Example
In the example above, we have a variable x initialized with the decimal value 16. In binary representation, it is 0001 0000. The code then applies the right shift operator (>>) to x with a shift value of 2. The right shift operation moves the bits of the number to the right by the specified number of positions.
public class Sample { public static void main(String[] args) { int x = 16; // binary: 0001 0000 int shifted = x >> 2; System.out.println(shifted); // Output: 4 } }
Output:
4
The resulting value is 4, which is the decimal representation of the binary number 0000 0100.
Here's how the right shift operation works for this example:
x: 0001 0000 (16) shifted: 0000 0100 (4)
The bits of num are shifted two positions to the right, and the vacant positions on the left are filled with zeros. Therefore, The resulting value is 4, which is the decimal representation of the binary number 0000 0100.
Bitwise Unsigned Right Shift Operator Example
In this example, we have a variable x initialized with the decimal value -10. In binary representation, it is 1111 1111 1111 1111 1111 1111 1111 0110. Since x is a signed integer, the leftmost bit (the sign bit) is set to 1 to indicate a negative value.
The code then applies the unsigned right shift operator (>>>) to x with a shift value of 2. The unsigned right shift operation moves the bits of the number to the right by the specified number of positions, filling the vacant positions on the left with zeros.
Example:
public class Sample { public static void main(String[] args) { int x = -10; // binary: 1111 1111 1111 1111 1111 1111 1111 0110 int shifted = x >>> 2; System.out.println(shifted); // Output: 1073741821 } }
Output:
1073741821
The resulting value is 1073741821, which is the decimal representation of the binary number 0011 1111 1111 1111 1111 1111 1111 1101.
Here's how the unsigned right shift operation works for this example:
x: 1111 1111 1111 1111 1111 1111 1111 0110 (-10) shifted: 0011 1111 1111 1111 1111 1111 1111 1101 (1073741821)
The bits of num are shifted two positions to the right, and the vacant positions on the left are filled with zeros. Therefore, The resulting value is 1073741821, which is the decimal representation of the binary number 0011 1111 1111 1111 1111 1111 1111 1101.