In Java, operators are used to perform operations on operands (variables or values). There are several types of operators in Java, including:
int a = 10, b = 5; int c = a + b; // c is 15 int d = a * b; // d is 50 int e = a % b; // e is 0 (since 10 is perfectly divisible by 5)
int a = 10; a += 5; // equivalent to a = a + 5; (a is now 15) a *= 2; // equivalent to a = a * 2; (a is now 30)
int a = 10, b = 5; boolean result = (a > b); // result is true result = (a == b); // result is false
boolean a = true, b = false, c = true; boolean result = (a && b); // result is false result = (a || b); // result is true result = !c; // result is false
int a = 0b1010, b = 0b1100; // 0b prefix is used to indicate binary numbers int result = (a & b); // result is 0b1000 (binary AND) result = (a | b); // result is 0b1110 (binary OR) result = (a ^ b); // result is 0b0110 (binary XOR) result = ~a; // result is 0b11111111111111111111111111110101 (binary NOT)
These are the most commonly used operators in Java. Understanding them is essential for writing effective Java programs.