Operators
Operators in Java are special symbols or keywords used to perform operations on variables and values. They are essential for manipulating data and building logical expressions in programs. Java supports a variety of operators, classified into different categories based on the type of operations they perform.
Types of Operators
- Arithmetic Operators
- Assignment Operators
- Relational (Comparison) Operators
- Logical Operators
- Unary Operators
- Bitwise Operators
- Conditional (Ternary) Operator
- Shift Operators
Arithmetic Operators
Arithmetic operators are used for performing mathematical operations like addition, subtraction, multiplication, etc.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
Example:
int a = 10;
int b = 5;
System.out.println(a + b); // Output: 15
System.out.println(a % b); // Output: 0
Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assigns value to a variable | a = 10 |
+= | Adds and assigns | a += 5 (same as a = a + 5 ) |
-= | Subtracts and assigns | a -= 5 (same as a = a - 5 ) |
*= | Multiplies and assigns | a *= 5 |
/= | Divides and assigns | a /= 5 |
%= | Modulus and assigns | a %= 5 |
Example:
int a = 10;
a += 5; // Same as a = a + 5
System.out.println(a); // Output: 15
Relational (Comparison) Operators
Relational operators are used to compare two values. The result of a comparison is a boolean value (true
or false
).
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Example:
int a = 10;
int b = 5;
System.out.println(a > b); // Output: true
System.out.println(a == b); // Output: false
Logical Operators
Logical operators are used to perform logical operations on boolean expressions.
Operator | Description | Example |
---|---|---|
&& | Logical AND (true if both are true) | a && b |
|| | Logical OR (true if at least one of the operands is true) | a || b |
! | Logical NOT (inverts the boolean value) | !a |
Example:
boolean a = true;
boolean b = false;
System.out.println(a && b); // Output: false
System.out.println(a || b); // Output: true
Unary Operators
Unary operators are used with only one operand. They perform operations like incrementing, decrementing, negating, or inverting a value.
Operator | Description | Example |
---|---|---|
+ | Unary plus (positive value) | +a |
- | Unary minus (negates value) | -a |
++ | Increment (increases value by 1) | ++a or a++ |
-- | Decrement (decreases value by 1) | --a or a-- |
! | Logical NOT (inverts the boolean value) | !a |
Example:
int a = 5;
System.out.println(++a); // Output: 6 (pre-increment)
System.out.println(a--); // Output: 6 (post-decrement, but now a is 5)
System.out.println(a); // Output: 5
int b = -a; // Unary minus
System.out.println(b); // Output: -5
Bitwise Operators
Bitwise operators are used to perform operations on individual bits of integer values.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
| | Bitwise OR | a | b |
^ | Bitwise XOR | ^a |
~ | Bitwise NOT | ~a |
Example:
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
System.out.println(a & b); // Output: 1 (0001 in binary)
System.out.println(a | b); // Output: 7 (0111 in binary)
System.out.println(a ^ b); // Output: 6 (0110 in binary)
System.out.println(~a); // Output: -6 (11111111 11111111 11111111 11111010 in binary)
Conditional (Ternary) Operator
The ternary operator is a shorthand for an if-else
statement. It is the only operator in Java that takes three operands.
Operator | Description | Example |
---|---|---|
? : | Ternary (conditional) operator | condition ? expr1 : expr2 |
Example:
int a = 5;
int b = 10;
int max = (a > b) ? a : b; // If a > b, max is a; otherwise, max is b
System.out.println(max); // Output: 10
Shift Operators
Shift operators shift the bits of a number to the left or right.
Operator | Description | Example |
---|---|---|
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
>>> | Unsigned right shift | a >>> 2 |
int a = 8; // 1000 in binary
System.out.println(a << 1); // Output: 16 (10000 in binary)
Left Shift Operator
The left shift operator shifts the bits of its operand to the left by the specified number of positions. The vacant positions on the right are filled with zeros.
Example:
int a = 5; // Binary: 0101
int result = a << 2; // Result: 10100 (decimal: 20)
Right Shift Operator
The signed right shift operator shifts the bits of its operand to the right. For positive numbers, the vacant positions on the left are filled with 0
, while for negative numbers, they are filled with 1
to preserve the sign.
Example:
int a = 20; // Binary: 00010100
int result = a >> 2; // Result: 00000101 (decimal: 5)
int b = -8; // Binary: 11111111 11111111 11111111 11111000
result = b >> 2; // Result: 11111111 11111111 11111111 11111110 (decimal: -2)
Unsigned Right Shift Operator
The unsigned right shift operator (>>>
) shifts the bits of a number to the right and fills the leftmost bits with zeros. It is used to shift the binary representation of an integer without preserving the sign (i.e., it treats the number as unsigned).
- The
>>>
operator shifts the bits to the right and fills the leftmost bits with zeros. - Unlike the signed right shift (
>>
), which maintains the sign bit for negative numbers,>>>
always inserts0
in the leftmost positions. - This is particularly useful when working with binary representations of data.
Example:
int num = -8; // Binary: 11111111 11111111 11111111 11111000
int result = num >>> 2; // Shift right by 2 bits
System.out.println(result); // Output: 1073741822
Explanation:
- The binary representation of
-8
is11111111 11111111 11111111 11111000
(32-bit signed integer). - After shifting 2 bits to the right using
>>>
, the result becomes:00111111 11111111 11111111 11111110
, which is the decimal value1073741822
.
Unsigned Right Shift(>>>
) vs Signed Right Shift (>>
):
- Signed right shift (
>>
): Preserves the sign bit (keeps negative numbers negative). - Unsigned right shift (
>>>
): Fills the leftmost bits with0
, ignoring the sign bit.
Example:
int num = -8;
System.out.println(num >> 2); // Output: -2 (signed shift)
System.out.println(num >>> 2); // Output: 1073741822 (unsigned shift)