Operators are special symbols or keywords that perform specific operations on operands (variables, literals, or expressions) and produce a result. Java supports various types of operators, which can be classified into different categories based on their functionality. Here are the commonly used operators in Java:
Addition (+): Adds two operands.
Subtraction (-): Subtracts the right operand from the left operand.
Multiplication (*): Multiplies two operands.
Division (/): Divides the left operand by the right operand.
Modulus (%): Returns the remainder after division.
Simple Assignment (=): Assigns a value to a variable.
Compound Assignment Operators (+=, -=, *=, /=, %=): Performs an arithmetic operation and assigns the result to the left operand.
Increment (++) and Decrement (--): Increases or decreases the value of a variable by 1.
Equal to (==): Checks if two operands are equal.
Not equal to (!=): Checks if two operands are not equal.
Greater than (>), Less than (<), Greater than or equal to (>=), Less than or equal to (<=): Perform comparison between operands.
Logical AND (&&): Returns true if both operands are true.
Logical OR (||): Returns true if either operand is true.
Logical NOT (!): Reverses the logical state of an operand.
Bitwise AND (&): Performs bitwise AND operation.
Bitwise OR (|): Performs bitwise OR operation.
Bitwise XOR (^): Performs bitwise XOR operation.
Bitwise NOT (~): Inverts the bits of an operand.
Left Shift (<<), Right Shift (>>): Shifts the bits of the left operand left or right by a specified number of positions.
Conditional ? : Checks a condition and returns one of two values based on the result.
These are the fundamental operators in Java. It's important to understand their usage and precedence rules when writing Java programs.
These operators are used for basic mathematical operations.
public class ArithmeticOperatorsExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
// Addition
int sum = a + b; // 10 + 5 = 15
// Subtraction
int difference = a - b; // 10 - 5 = 5
// Multiplication
int product = a * b; // 10 * 5 = 50
// Division
int quotient = a / b; // 10 / 5 = 2
// Modulo (Remainder)
int remainder = a % b; // 10 % 5 = 0
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
System.out.println("Remainder: " + remainder);
}
}
These operators are used to assign values to variables.
public class AssignmentOperatorsExample {
public static void main(String[] args) {
int a = 10;
// Assignment
int b = a; // b gets the value of a (b = 10)
// Addition assignment
int c = 5;
c += a; // Equivalent to: c = c + a (c = 5 + 10 = 15)
// Subtraction assignment
int d = 20;
d -= a; // Equivalent to: d = d - a (d = 20 - 10 = 10)
// Multiplication assignment
int e = 2;
e *= a; // Equivalent to: e = e * a (e = 2 * 10 = 20)
// Division assignment
int f = 30;
f /= a; // Equivalent to: f = f / a (f = 30 / 10 = 3)
// Modulo assignment
int g = 25;
g %= a; // Equivalent to: g = g % a (g = 25 % 10 = 5)
System.out.println("b: " + b);
System.out.println("c: " + c);
System.out.println("d: " + d);
System.out.println("e: " + e);
System.out.println("f: " + f);
System.out.println("g: " + g);
}
}
These operators are used to compare values and return a boolean result (true or false).
public class ComparisonOperatorsExample {
public static void main(String[] args) {
int a = 10;
int b = 5;
// Equal to
boolean isEqual = (a == b); // false
// Not equal to
boolean isNotEqual = (a != b); // true
// Greater than
boolean isGreater = (a > b); // true
// Less than
boolean isLess = (a < b); // false
// Greater than or equal to
boolean isGreaterOrEqual = (a >= b); // true
// Less than or equal to
boolean isLessOrEqual = (a <= b); // false
System.out.println("isEqual: " + isEqual);
System.out.println("isNotEqual: " + isNotEqual);
System.out.println("isGreater: " + isGreater);
System.out.println("isLess: " + isLess);
System.out.println("isGreaterOrEqual: " + isGreaterOrEqual);
System.out.println("isLessOrEqual: " + isLessOrEqual);
}
}
These operators are used to perform logical operations on boolean values.
public class LogicalOperatorsExample {
public static void main(String[] args) {
boolean isJavaFun = true;
boolean isLearning = false;
// Logical AND
boolean result1 = isJavaFun && isLearning; // false
// Logical OR
boolean result2 = isJavaFun || isLearning; // true
// Logical NOT
boolean result3 = !isJavaFun; // false
System.out.println("result1: " + result1);
System.out.println("result2: " + result2);
System.out.println("result3: " + result3);
}
}
These operators perform bitwise operations on integer types at the bit level.
public class BitwiseOperatorsExample {
public static void main(String[] args) {
int a = 5; // binary: 0000 0101
int b = 3; // binary: 0000 0011
// Bitwise AND
int result1 = a & b; // binary: 0000 0001 (1 in decimal)
// Bitwise OR
int result2 = a | b; // binary: 0000 0111 (7 in decimal)
// Bitwise XOR
int result3 = a ^ b; // binary: 0000 0110 (6 in decimal)
// Bitwise NOT
int result4 = ~a; // binary: 1111 1010 (-6 in decimal)
// Left Shift
int result5 = a << 1; // binary: 0000 1010 (10 in decimal)
// Right Shift
int result6 = a >> 1; // binary: 0000 0010 (2 in decimal)
System.out.println("result1: " + result1);
System.out.println("result2: " + result2);
System.out.println("result3: " + result3);
System.out.println("result4: " + result4);
System.out.println("result5: " + result5);
System.out.println("result6: " + result6);
}
}