Java Operators
Complete guide to Java operators with examples. Learn arithmetic, assignment, relational, logical, unary, bitwise operators and more.
Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while * is also an operator used for multiplication.
Operators in Java can be classified into 6 types:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Unary Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
| Operator | Name | Description | Example |
|---|---|---|---|
+ | Addition | Adds together two values | x + y |
- | Subtraction | Subtracts one value from another | x - y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides one value by another | x / y |
% | Modulus | Returns the division remainder | x % y |
Example
int x = 10;
int y = 3;
System.out.println(x + y); // 13
System.out.println(x / y); // 3 (Integer division!)
System.out.println(x % y); // 1 (Remainder)2. Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Example | Same As |
|---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
3. Relational Operators
Relational operators are used to compare two values. They return a boolean result (true or false).
| Operator | Name | Example |
|---|---|---|
== | Equal to | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
4. Logical Operators
Logical operators are used to determine the logic between variables or values.
| Operator | Name | Description | Example |
|---|---|---|---|
&& | Logical AND | Returns true if both statements are true | x < 5 && x < 10 |
|| | Logical OR | Returns true if one of the statements is true | x < 5 || x < 4 |
! | Logical NOT | Reverse the result, returns false if the result is true | !(x < 5 && x < 10) |
5. Unary Operators
Unary operators are used with only one operand.
| Operator | Description | Example |
|---|---|---|
++ | Increment (increases value by 1) | ++x or x++ |
-- | Decrement (decreases value by 1) | --x or x-- |
Prefix vs Postfix
- Prefix (
++x): Increments the variable, then uses the new value. - Postfix (
x++): Uses the current value, then increments the variable.
int a = 5;
int b = ++a; // a becomes 6, b becomes 6
int c = 5;
int d = c++; // d becomes 5, c becomes 66. Bitwise Operators
Bitwise operators work on bits and perform bit-by-bit operations.
| Operator | Name | Description |
|---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shift left by pushing zeros in from the right |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left |
Operator Precedence
Java has well-defined rules for specifying the order in which the operators in an expression are evaluated.
| Category | Operators |
|---|---|
| Postfix | expr++ expr-- |
| Unary | ++expr --expr +expr -expr ~ ! |
| Multiplicative | * / % |
| Additive | + - |
| Shift | << >> >>> |
| Relational | < > <= >= instanceof |
| Equality | == != |
| Bitwise AND | & |
| Bitwise XOR | ^ |
| Bitwise OR | | |
| Logical AND | && |
| Logical OR | || |
| Ternary | ? : |
| Assignment | = += -= *= /= %= |
Key Takeaways
- Arithmetic:
+,-,*,/,%(remainder). - Relational:
==,!=,>,<,>=,<=(return boolean). - Logical:
&&(AND),||(OR),!(NOT). - Assignment:
=,+=,-=, etc.
Common Pitfalls
[!WARNING] Equality Check: Use
==to compare primitives. NEVER use==to compare Strings (use.equals()instead).
[!WARNING] Precedence:
*and/happen before+and-. Use parentheses()to control the order of operations.
Challenge
Challenge
Task:
Calculate the sum of 15 and 25 and print the result.