Java Operators
In Java, operators are special symbols that perform operations on variables and values. Operators can be classified into several categories based on their functionality. Understanding these operators is crucial for performing calculations, comparisons, and manipulations of data in Java programming.
Types of Operators in Java
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Unary Operators
- Ternary Operator (Conditional Operator)
- Instanceof Operator
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and modulus.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
Example:
public class ArithmeticOperatorsExample {
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
}
}
2. Relational Operators
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 |
Example:
public class RelationalOperatorsExample {
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
}
}
3. Logical Operators
Logical operators are used to perform logical operations, often with boolean values.
Operator | Description | Example |
---|---|---|
&& | Logical AND | (a > b) && (a < c) |
` | ` | |
! | Logical NOT | !(a > b) |
Example:
public class LogicalOperatorsExample {
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
}
}
4. Bitwise Operators
Bitwise operators perform operations on bits and are used for bit-level manipulation.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
>>> | Unsigned right shift | a >>> 2 |
Example:
public class BitwiseOperatorsExample {
public static void main(String[] args) {
int a = 5, b = 3; // In binary: a = 0101, b = 0011
System.out.println("Bitwise AND: " + (a & b)); // 1 (0001)
System.out.println("Bitwise OR: " + (a | b)); // 7 (0111)
System.out.println("Bitwise XOR: " + (a ^ b)); // 6 (0110)
System.out.println("Bitwise NOT: " + (~a)); // -6
}
}
5. Assignment Operators
Assignment operators are used to assign values to variables. The most common assignment operator is =
. There are also compound assignment operators that combine assignment with another operation.
Operator | Description | Example |
---|---|---|
= | Simple 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 |
Example:
public class AssignmentOperatorsExample {
public static void main(String[] args) {
int a = 10;
a += 5; // equivalent to a = a + 5
System.out.println("Value of a: " + a); // 15
}
}
6. Unary Operators
Unary operators operate on a single operand and can be used to perform various operations.
Operator | Description | Example |
---|---|---|
+ | Unary plus | +a |
- | Unary minus (negation) | -a |
++ | Increment | ++a or a++ |
-- | Decrement | --a or a-- |
Example:
public class UnaryOperatorsExample {
public static void main(String[] args) {
int a = 5;
System.out.println("Original a: " + a); // 5
System.out.println("Incremented a: " + ++a); // 6
System.out.println("Decremented a: " + --a); // 5
}
}
7. Ternary Operator (Conditional Operator)
The ternary operator is a shorthand way of expressing conditional statements. It takes three operands.
Syntax:
condition ? expression1 : expression2
If the condition is true, expression1
is evaluated; otherwise, expression2
is evaluated.
Example:
public class TernaryOperatorExample {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b; // Assigns the maximum of a and b
System.out.println("Maximum: " + max); // 20
}
}
8. Instanceof Operator
The instanceof
operator is used to test whether an object is an instance of a specific class or subclass.
Example:
public class InstanceofOperatorExample {
public static void main(String[] args) {
String name = "Alice";
boolean result = name instanceof String; // true
System.out.println("Is name a String? " + result);
}
}
Conclusion
Java provides a rich set of operators that enable various operations on variables and values. Understanding how to use these operators effectively is crucial for writing efficient and clear Java code. Each type of operator serves a specific purpose, and knowing when and how to use them can greatly enhance your programming capabilities.