In Java, operators are special symbols used to perform operations on variables and values. They are essential components of any programming language, and understanding how they work is crucial for writing efficient and effective code. Java offers a wide range of operators that allow you to perform arithmetic, comparison, logical, bitwise, and other operations.
This blog will guide you through the different types of operators in Java, explain how they work, and provide sample code to help you understand their usage.
Java operators are symbols that are used to perform operations on variables and values. These operations can range from simple arithmetic (like addition and subtraction) to more complex logical checks (like AND/OR operations). Operators can be categorized based on their functionality and the type of data they operate on.
Java provides a rich set of operators, and understanding their use is fundamental to building robust and efficient applications.
Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, and division.
Operator | Description | Example |
---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus (remainder) | a % b |
public class ArithmeticOperators {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Addition: " + (a + b)); // 15
System.out.println("Subtraction: " + (a - b)); // 5
System.out.println("Multiplication: " + (a * b)); // 50
System.out.println("Division: " + (a / b)); // 2
System.out.println("Modulus: " + (a % b)); // 0
}
}
Relational operators are used to compare two values. They return a boolean result (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 |
public class RelationalOperators {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Is a equal to b? " + (a == b)); // false
System.out.println("Is a not equal to b? " + (a != b)); // true
System.out.println("Is a greater than b? " + (a > b)); // true
System.out.println("Is a less than b? " + (a < b)); // false
}
}
Logical operators are used to perform logical operations, often in conjunction with boolean values.
Operator | Description | Example |
---|---|---|
&& |
Logical AND | a && b |
` | ` | |
! |
Logical NOT | !a |
public class LogicalOperators {
public static void main(String[] args) {
boolean a = true, b = false;
System.out.println("a AND b: " + (a && b)); // false
System.out.println("a OR b: " + (a || b)); // true
System.out.println("NOT a: " + !a); // false
}
}
Bitwise operators are used to perform operations on individual bits of data.
Operator | Description | Example |
---|---|---|
& |
AND | a & b |
` | ` | OR |
^ |
XOR | a ^ b |
~ |
NOT | ~a |
<< |
Left shift | a << 2 |
>> |
Right shift | a >> 2 |
>>> |
Unsigned right shift | a >>> 2 |
public class BitwiseOperators {
public static void main(String[] args) {
int a = 5, b = 3; // 5 = 0101, 3 = 0011
System.out.println("a AND b: " + (a & b)); // 1 (0001)
System.out.println("a OR b: " + (a | b)); // 7 (0111)
System.out.println("a XOR b: " + (a ^ b)); // 6 (0110)
System.out.println("a Left Shift: " + (a << 2)); // 20 (10100)
System.out.println("a Right Shift: " + (a >> 2)); // 1 (0001)
}
}
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= |
Assignment | a = b |
+= |
Add and assign | a += b |
-= |
Subtract and assign | a -= b |
*= |
Multiply and assign | a *= b |
/= |
Divide and assign | a /= b |
%= |
Modulus and assign | a %= b |
public class AssignmentOperators {
public static void main(String[] args) {
int a = 10, b = 5;
a += b; // a = a + b
System.out.println("a += b: " + a); // 15
a *= b; // a = a * b
System.out.println("a *= b: " + a); // 75
}
}
Unary operators are used with a single operand to perform operations like incrementing, decrementing, negating, etc.
Operator | Description | Example |
---|---|---|
++ |
Increment | a++ |
-- |
Decrement | a-- |
+ |
Unary plus | +a |
- |
Unary minus | -a |
! |
Logical NOT | !a |
public class UnaryOperators {
public static void main(String[] args) {
int a = 10;
System.out.println("Increment: " + ++a); // 11
System.out.println("Decrement: " + --a); // 10
System.out.println("Unary minus: " + -a); // -10
System.out.println("Logical NOT: " + !true); // false
}
}
The ternary operator is a shorthand for the if-else
statement.
Operator | Description | Example |
---|---|---|
?: |
Ternary conditional operator | condition ? value_if_true : value_if_false |
public class TernaryOperator {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max value: " + max); // 20
}
}
The instanceof
operator checks whether an object is an instance of a specific class or subclass.
Operator | Description | Example |
---|---|---|
instanceof |
Type check | a instanceof ClassName |
public class InstanceofOperator {
public static void main(String[] args) {
String str = "Hello";
System.out.println(str instanceof String); // true
}
}
Operator precedence determines the order in which operators are evaluated in an expression. Java follows specific rules for operator precedence and associativity. For example, multiplication (*
) and division (/
) have higher precedence than addition (+
) and subtraction (-
).
public class OperatorsExample {
public static void main(String[] args) {
int a = 10, b = 5;
// Arithmetic operators
System.out.println("a + b: " + (a + b));
System.out.println("a - b: " + (a - b));
// Relational operators
System.out.println("a == b: " + (a == b));
// Logical operators
System.out.println("a > b && b < a: " + (a > b && b < a));
// Assignment operators
a += 5;
System.out.println("a after a += 5: " + a);
}
}
if-else
logic, consider using the ternary operator for cleaner code.