Operators are symbols that perform operations on variables and values. Java supports several types of operators:
These are used to compare two values. The result is a boolean (true or false).
| Operator | Operator | 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 |
xxxxxxxxxxpublic class RelationalExample { public static void main(String[] args) { int a = 10; int b = 5; System.out.println("a == b: " + (a == b)); System.out.println("a != b: " + (a != b)); System.out.println("a > b: " + (a > b)); System.out.println("a < b: " + (a < b)); System.out.println("a >= b: " + (a >= b)); System.out.println("a <= b: " + (a <= b)); }}a == b: false
a != b: true
a > b: true
a < b: false
a >= b: true
a <= b: falseThese are used to perform basic arithmetic operations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | 5 + 3 = 8 |
| - | Subtraction | 5 - 3 = 2 |
| * | Multiplication | 5 * 3 = 15 |
| / | Division | 15 / 3 = 5 |
| % | Modulus (remainder) | 5 % 3 = 2 |
xxxxxxxxxxpublic class ArithmeticExample { public static void main(String[] args) { int a = 10; int b = 3; 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("Modulus: " + (a % b)); }}Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1These are used to perform logical operations on boolean expressions.
| Operator | Description | Example |
|---|---|---|
| || | Logical OR | condition1 || condition2 |
| && | Logical AND | a && b |
| ! | Logical NOT | !a |
public class LogicalExample { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println("a && b: " + (a && b)); // false System.out.println("a || b: " + (a || b)); // true System.out.println("!a: " + (!a)); // false }}a && b: false
a || b: true
!a: falseThese are used to assign values to variables.
| Operator | Description | Example |
|---|---|---|
| = | Assign | a = 10 |
| += | Add and assign | a += 5 (same as a = a + 5) |
| -= | Subtract and assign | a -= 3 |
| *= | Multiply and assign | a *= 2 |
| /= | Divide and assign | a /= 2 |
xxxxxxxxxxpublic class AssignmentExample { public static void main(String[] args) { int a = 10; a += 5; System.out.println("a += 5: " + a); a -= 3; System.out.println("a -= 3: " + a); a *= 2; System.out.println("a *= 2: " + a); a /= 2; System.out.println("a /= 2: " + a); }}a += 5: 15
a -= 3: 12
a *= 2: 24
a /= 2: 12The bitwise operators in Java allow you to manipulate individual bits of data types like integers and bytes. Bitwise operations are different from logical operators (&&, ||) because they operate directly on the binary representations of integers rather than evaluating boolean expressions.
The bitwise AND operator (&) performs a binary AND operation on two integer values, comparing each bit of the two integers. The result is a binary value where a bit is set to 1 only if both corresponding bits in the two operands are 1. If either bit is 0, the resulting bit is 0.
Syntax:
result = operand1 & operand2;
Bitwise AND Operation:
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0xxxxxxxxxxpublic class BitwiseANDExample { public static void main(String[] args) { int a = 6; // Binary: 0110 int b = 3; // Binary: 0011 int result = a & b; // Perform bitwise AND System.out.println("Result of a & b: " + result); // Output: 2 }}Explanation:
0110 (6 in binary)
&
0011 (3 in binary)
------
0010 (Result = 2)
The bitwise OR operator (|) performs a binary OR operation on two integer values, comparing each bit of the two operands. The result is a binary value where a bit is set to 1 if either of the corresponding bits in the two operands is 1. If both bits are 0, the resulting bit is 0.
Syntax:
result = operand1 | operand2;
Bitwise OR Operation:
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0xxxxxxxxxxpublic class BitwiseORExample { public static void main(String[] args) { int a = 6; // Binary: 0110 int b = 3; // Binary: 0011 int result = a | b; // Perform bitwise OR System.out.println("Result of a | b: " + result); // Output: 7 }}Explanation:
a = 6 is 0110 in binary.b = 3 is 0011 in binary.0110 | 0011 results in 0111, which is 7 in decimal.0110 (6 in binary)
|
0011 (3 in binary)
------
0111 (Result = 7)