Java Core: Operators
Operators are special symbols that perform specific operations on operands (values or variables). Java provides a rich set of operators to perform arithmetic, logical, relational, bitwise, and other operations.
1. Arithmetic Operators
These operators perform mathematical calculations.
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 5 / 3 |
1 (Integer division truncates the decimal part) |
% |
Modulus (Remainder) | 5 % 3 |
2 |
++ |
Increment (Adds 1 to the operand) | int x = 5; x++; |
x becomes 6 |
-- |
Decrement (Subtracts 1 from the operand) | int x = 5; x--; |
x becomes 4 |
Important Notes:
- Division (
/) with integers results in integer division (truncating the decimal part). To get a floating-point result, at least one operand must be a floating-point type (e.g.,5.0 / 3). ++and--can be used as prefix (++x) or postfix (x++) operators. The difference lies in when the increment/decrement happens relative to the expression's evaluation.- Prefix: Increment/decrement before the value is used in the expression.
- Postfix: Increment/decrement after the value is used in the expression.
int x = 5;
int y = x++; // y = 5, x = 6 (postfix)
int a = 5;
int b = ++a; // a = 6, b = 6 (prefix)
2. Assignment Operators
These operators assign values to variables.
| Operator | Description | Example | Equivalent to |
|---|---|---|---|
= |
Assignment | x = 5; |
|
+= |
Add and Assign | x += 5; |
x = x + 5; |
-= |
Subtract and Assign | x -= 5; |
x = x - 5; |
*= |
Multiply and Assign | x *= 5; |
x = x * 5; |
/= |
Divide and Assign | x /= 5; |
x = x / 5; |
%= |
Modulus and Assign | x %= 5; |
x = x % 5; |
3. Relational Operators
These operators compare two operands and return a boolean value (true or false).
| Operator | Description | Example | Result (if x = 5, y = 10) |
|---|---|---|---|
== |
Equal to | x == y |
false |
!= |
Not equal to | x != y |
true |
> |
Greater than | x > y |
false |
< |
Less than | x < y |
true |
>= |
Greater than or equal to | x >= y |
false |
<= |
Less than or equal to | x <= y |
true |
4. Logical Operators
These operators combine boolean expressions.
| Operator | Description | Example | Result (if x = true, y = false) |
|---|---|---|---|
&& |
Logical AND | x && y |
false (true only if both operands are true) |
| ` | ` | Logical OR | |
! |
Logical NOT | !x |
false (inverts the boolean value) |
Short-Circuiting:
Java's logical operators use short-circuiting. This means:
&&: If the left operand isfalse, the right operand is not evaluated.||: If the left operand istrue, the right operand is not evaluated.
5. Bitwise Operators
These operators work on the individual bits of integer operands.
| Operator | Description | Example |
|---|---|---|
& |
Bitwise AND | 5 & 3 (0101 & 0011 = 0001 = 1) |
| ` | ` | Bitwise OR |
^ |
Bitwise XOR (Exclusive OR) | 5 ^ 3 (0101 ^ 0011 = 0110 = 6) |
~ |
Bitwise NOT (One's Complement) | ~5 (Inverts all bits) |
<< |
Left Shift | 5 << 2 (0101 << 2 = 10100 = 20) |
>> |
Right Shift (Sign-Extending) | 5 >> 2 (0101 >> 2 = 0001 = 1) |
>>> |
Right Shift (Zero-Extending) | 5 >>> 2 (0101 >>> 2 = 0001 = 1) |
6. Ternary Operator (Conditional Operator)
This is a shorthand for an if-else statement.
| Operator | Description | Example |
|---|---|---|
? : |
Conditional | int result = (x > y) ? x : y; (If x > y, result = x; otherwise, result = y) |
7. Operator Precedence
Operators have a precedence order that determines the order in which operations are performed. Here's a simplified order (highest to lowest):
- Parentheses
() - Unary operators (
++,--,!,~) - Multiplication, Division, Modulus (
*,/,%) - Addition, Subtraction (
+,-) - Shift operators (
<<,>>,>>>) - Relational operators (
==,!=,>,<,>=,<=) - Logical operators (
&&,||) - Ternary operator
? : - Assignment operators (
=,+=,-=, etc.)
Use parentheses to explicitly control the order of operations when needed.
Example:
int result = 5 + 3 * 2; // result = 11 (Multiplication before addition)
int result2 = (5 + 3) * 2; // result2 = 16 (Addition before multiplication)
This comprehensive overview should give you a solid understanding of operators in Java. Remember to practice using them to become comfortable with their behavior.