Table of Contents
- Introduction to Operators in Java
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Assignment Operators
- Unary Operators
- Bitwise Operators (Bonus Section)
- Operator Precedence and Associativity
- Common Mistakes to Avoid
- Real-World Use Cases and Examples
- Summary and What’s Next
1. Introduction to Operators in Java
In Java, operators are special symbols or keywords used to perform operations on variables and values. They are the backbone of any expression and play a critical role in controlling program logic and data manipulation. Java includes a wide range of operators to support arithmetic calculations, logical decision-making, and even low-level bit manipulation.
Operators work with operands. For example, in the expression a + b
, the +
operator works on operands a
and b
.
Operators can be categorized based on their functionality and the number of operands they work with:
- Unary Operators (one operand)
- Binary Operators (two operands)
- Ternary Operator (three operands, covered in the next module)
2. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations. They always return numeric results and can be used with all numeric primitive types (int, float, double, etc.).
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2 |
% | Modulus (remainder) | 10 % 3 | 1 |
Integer Division vs Floating-point Division
When you divide two integers in Java, the result is also an integer (the decimal part is truncated):
int result = 7 / 2; // result = 3
To get an accurate floating-point result, cast one operand to a float or double:
double result = 7 / 2.0; // result = 3.5
3. Relational (Comparison) Operators
Relational operators compare two values and return a boolean result. These are essential in control flow and decision-making.
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 4 | true |
> | Greater than | 5 > 3 | true |
< | Less than | 5 < 3 | false |
>= | Greater than or equal to | 5 >= 5 | true |
<= | Less than or equal to | 4 <= 5 | true |
These are primarily used in if
, while
, for
, and switch
statements.
4. Logical Operators
Logical operators allow combining multiple conditions. They work with boolean values only.
Operator | Description | Syntax | Example |
---|---|---|---|
&& | Logical AND | condition1 && condition2 | true && false → false |
` | ` | Logical OR | |
! | Logical NOT | !condition | !true → false |
Short-circuiting Behavior
Java uses short-circuit evaluation:
&&
: If the first operand is false, the second operand is not evaluated.||
: If the first operand is true, the second operand is not evaluated.
This behavior improves performance and avoids potential exceptions, such as NullPointerException
.
5. Assignment Operators
Assignment operators are used to assign values to variables. Java supports shorthand operators for updating values.
Operator | Description | Example | Equivalent |
---|---|---|---|
= | Assign | a = 10 | — |
+= | Add and assign | a += 5 | a = a + 5 |
-= | Subtract and assign | a -= 3 | a = a - 3 |
*= | Multiply and assign | a *= 2 | a = a * 2 |
/= | Divide and assign | a /= 4 | a = a / 4 |
%= | Modulus and assign | a %= 3 | a = a % 3 |
These are helpful in loops and arithmetic-heavy programs.
6. Unary Operators
Unary operators work with a single operand.
Operator | Description | Example | Result |
---|---|---|---|
+ | Unary plus (no effect) | +a | a |
- | Unary minus | -a | Negates a |
++ | Increment (prefix/postfix) | ++a , a++ | Increments a |
-- | Decrement (prefix/postfix) | --a , a-- | Decrements a |
! | Logical complement | !true | false |
Prefix vs Postfix
int a = 5;
int b = ++a; // b = 6, a = 6 (prefix)
int c = a++; // c = 6, a = 7 (postfix)
7. Bitwise Operators (Bonus Section)
Bitwise operators are used for performing operations at the bit level, primarily useful in low-level programming and optimization.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
Example:
int x = 5; // 0101 in binary
int y = 3; // 0011 in binary
System.out.println(x & y); // Output: 1 (0001)
8. Operator Precedence and Associativity
When multiple operators are used in an expression, Java evaluates them based on precedence and associativity.
Precedence (High to Low)
- Postfix:
expr++
,expr--
- Unary:
++expr
,--expr
,+
,-
,!
- Multiplicative:
*
,/
,%
- Additive:
+
,-
- Relational:
<
,<=
,>
,>=
- Equality:
==
,!=
- Logical AND:
&&
- Logical OR:
||
- Assignment:
=
,+=
,-=
, etc.
Associativity
- Most binary operators are left-to-right associative.
- Assignment operators are right-to-left associative.
Use parentheses to make expressions clear and override default precedence.
9. Common Mistakes to Avoid
- Using
=
instead of==
in conditions (if (a = b)
instead ofif (a == b)
) - Forgetting short-circuit behavior in
&&
and||
- Integer division errors due to type mismatch
- Using post-increment when pre-increment is needed (or vice versa)
- Applying logical operators to non-boolean operands
10. Real-World Use Cases and Examples
- Use relational and logical operators for input validation:
if (age >= 18 && citizenship.equals("Indian")) { System.out.println("Eligible to vote."); }
- Arithmetic operators in billing applications:
double total = quantity * pricePerUnit + tax;
- Bitwise operations in graphics and cryptography
- Assignment operators in loops to update counters
11. Summary and What’s Next
In this module, you have covered:
- Different categories of Java operators
- Syntax and examples for arithmetic, relational, logical, and assignment operators
- Operator precedence and associativity
- Common mistakes and real-world use cases
Understanding how and when to use each type of operator helps you write cleaner and more efficient code.