Operators in Java
Introduction
An operator is a symbol that tells Java to perform an operation on one or more values or variables.
For example:
int a = 10; int b = 5; int sum = a + b;
Here, + is an arithmetic operator used to add two values.
Types of Operators in Java
Java provides several types of operators:
Operators │ ├── Arithmetic Operators ├── Assignment Operators ├── Relational Operators ├── Logical Operators ├── Unary Operators ├── Bitwise Operators ├── Shift Operators ├── Ternary Operator └── instanceof Operator
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus/Remainder | a % b |
Example
public class Main { public static void main(String[] args) { int a = 10; int b = 3; System.out.println(a + b); System.out.println(a - b); System.out.println(a * b); System.out.println(a / b); System.out.println(a % b); } }
Output:
13 7 30 3 1
Note: When both operands are integers, / performs integer division, so 10 / 3 gives 3.
2. Assignment Operators
Assignment operators are used to assign or update values in variables.
| Operator | Example | Equivalent |
|---|---|---|
= | a = 10 | a = 10 |
+= | a += 5 | a = a + 5 |
-= | a -= 5 | a = a - 5 |
*= | a *= 5 | a = a * 5 |
/= | a /= 5 | a = a / 5 |
%= | a %= 5 | a = a % 5 |
Example
int a = 10; a += 5; System.out.println(a);
Output:
15
3. Relational Operators
Relational operators compare two values and produce a boolean result: true or false.
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example
int a = 10; int b = 5; System.out.println(a > b); System.out.println(a == b); System.out.println(a != b);
Output:
true false true
4. Logical Operators
Logical operators are used to combine or reverse boolean expressions.
| Operator | Name | Meaning | ||
|---|---|---|---|---|
&& | Logical AND | Both conditions must be true | ||
| ` | ` | Logical OR | At least one condition must be true | |
! | Logical NOT | Reverses the boolean value |
Logical AND
int age = 20; System.out.println(age >= 18 && age <= 60);
Output:
true
Logical OR
int age = 16; System.out.println(age < 18 || age > 60);
Output:
true
Logical NOT
boolean passed = true; System.out.println(!passed);
Output:
false
5. Unary Operators
Unary operators work with only one operand.
Common unary operators are:
+-++--!~
Increment Operator ++
It increases a value by 1.
int a = 10; a++; System.out.println(a);
Output:
11
Decrement Operator --
It decreases a value by 1.
int a = 10; a--; System.out.println(a);
Output:
9
6. Pre-Increment and Post-Increment
Pre-Increment
The value is increased before it is used.
int a = 10; System.out.println(++a);
Output:
11
Post-Increment
The original value is used first, then it is increased.
int a = 10; System.out.println(a++); System.out.println(a);
Output:
10 11
7. Bitwise Operators
Bitwise operators work at the level of individual bits of integer types.
| Operator | Meaning | |
|---|---|---|
& | Bitwise AND | |
| ` | ` | Bitwise OR |
^ | Bitwise XOR | |
~ | Bitwise Complement |
Example
int a = 5; int b = 3; System.out.println(a & b); System.out.println(a | b); System.out.println(a ^ b);
Binary representation:
5 = 0101 3 = 0011
For example:
0101 0011 ---- 0001
Therefore:
5 & 3 = 1
8. Shift Operators
Shift operators move the bits of an integer left or right.
| Operator | Meaning |
|---|---|
<< | Left shift |
>> | Signed right shift |
>>> | Unsigned right shift |
Left Shift
int a = 5; System.out.println(a << 1);
Output:
10
Right Shift
int a = 10; System.out.println(a >> 1);
Output:
5
9. Ternary Operator
The ternary operator is a short form of if-else.
Syntax
condition ? valueIfTrue : valueIfFalse;
Example
int age = 20; String result = (age >= 18) ? "Adult" : "Minor"; System.out.println(result);
Output:
Adult
It is useful for simple conditional expressions.
10. instanceof Operator
The instanceof operator checks whether an object is an instance of a particular class or compatible type.
Example
String name = "Anuradha"; System.out.println(name instanceof String);
Output:
true
It returns a boolean value.
11. String Concatenation Operator
The + operator can also be used to join strings.
String firstName = "Anuradha"; String lastName = "Mandal"; String fullName = firstName + " " + lastName; System.out.println(fullName);
Output:
Anuradha Mandal
When one operand is a String, + performs string concatenation.
12. Operator Precedence
When an expression contains multiple operators, Java follows operator precedence rules.
For example:
int result = 10 + 5 * 2;
Multiplication is performed before addition.
5 * 2 = 10 10 + 10 = 20
Therefore:
result = 20
Parentheses can be used to change the order:
int result = (10 + 5) * 2;
Now:
15 * 2 = 30
Therefore:
result = 30
13. Common Operator Precedence
A simplified order from higher to lower precedence is:
1. () Parentheses 2. ++ -- ! ~ Unary operators 3. * / % Multiplication, division, modulus 4. + - Addition, subtraction 5. << >> >>> Shift operators 6. < <= > >= Relational operators 7. == != Equality operators 8. & Bitwise AND 9. ^ Bitwise XOR 10. | Bitwise OR 11. && Logical AND 12. || Logical OR 13. ?: Ternary 14. = += -= ... Assignment
14. Complete Example
public class Main { public static void main(String[] args) { int a = 20; int b = 10; // Arithmetic System.out.println("Addition: " + (a + b)); System.out.println("Subtraction: " + (a - b)); System.out.println("Multiplication: " + (a * b)); System.out.println("Division: " + (a / b)); System.out.println("Remainder: " + (a % b)); // Relational System.out.println("a > b: " + (a > b)); // Logical System.out.println("Both positive: " + (a > 0 && b > 0)); // Assignment a += 5; System.out.println("New a: " + a); // Ternary String result = (a > b) ? "a is greater" : "b is greater"; System.out.println(result); } }
Output:
Addition: 30 Subtraction: 10 Multiplication: 200 Division: 2 Remainder: 0 a > b: true Both positive: true New a: 25 a is greater
Summary
| Operator Type | Main Operators | ||
|---|---|---|---|
| Arithmetic | + - * / % | ||
| Assignment | = += -= *= /= %= | ||
| Relational | == != > < >= <= | ||
| Logical | `&& | !` | |
| Unary | + - ++ -- ! ~ | ||
| Bitwise | `& | ^ ~` | |
| Shift | << >> >>> | ||
| Ternary | ?: | ||
| Type checking | instanceof |
Conclusion
Operators are essential in Java programming because they allow us to calculate values, compare data, assign values, perform logical operations, manipulate bits, and make decisions. Understanding operators and their precedence is important for writing correct Java expressions and programs.