Java Operators
• Java operators are symbols or keywords that perform
operations on variables and values. They are broadly
categorized into the following types:
• 1 - Arithmetic Operators
• 2 - Relational (Comparison) Operators
• 3 - Logical Operators
• 4 - Assignment Operators
• 5 - Unary Operators
• 6 - Bitwise Operators
• 7 - Ternary Operator
• 8 - Instanceof Operator
• 1. Arithmetic Operators
• Used to perform basic mathematical operations.
• Example-
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 5;
[Link]("Addition: " + (a + b)); // 15
[Link]("Subtraction: " + (a - b)); // 5
[Link]("Multiplication: " + (a * b)); // 50
[Link]("Division: " + (a / b)); // 2
[Link]("Modulus: " + (a % b)); // 0
}
}
• 2. Relational (Comparison) Operators
• Used to compare two values.
• Example-
public class RelationalExample {
public static void main(String[] args) {
int a = 10, b = 5;
[Link]("Equal to: " + (a == b)); // false
[Link]("Not equal to: " + (a != b)); // true
[Link]("Greater than: " + (a > b)); // true
[Link]("Less than: " + (a < b)); // false
[Link]("Greater than or equal to: " + (a >= b)); // true
[Link]("Less than or equal to: " + (a <= b)); // false
}
}
• 3. Logical Operators
• Used for logical operations.
• Example-
public class LogicalExample {
public static void main(String[] args) {
int a = 10, b = 5, c = 15;
// Logical AND: true if both conditions are true
[Link]("Logical AND: " + ((a > b) && (c > a))); // true
// Logical OR: true if at least one condition is true
[Link]("Logical OR: " + ((a < b) || (c > a))); // true
// Logical NOT: reverses the truth value
[Link]("Logical NOT: " + !(a > b)); // false
}
}
• 4. Assignment Operators
• Used to assign values to variables.
• Example –
public class AssignmentExample {
public static void main(String[] args) {
int a = 10;
a += 5; // a = a + 5
[Link]("a after +=: " + a); // 15
a -= 2; // a = a - 2
[Link]("a after -=: " + a); // 13
a *= 2; // a = a * 2
[Link]("a after *=: " + a); // 26
a /= 2; // a = a / 2
[Link]("a after /=: " + a); // 13
}
}
• 5. Unary Operators
• Operate on a single operand.
• Example –
public class UnaryExample {
public static void main(String[] args) {
int a = 10;
[Link]("Post-increment: " + a++); // 10 (prints, then increments)
[Link]("After Post-increment: " + a); // 11
[Link]("Pre-increment: " + ++a); // 12 (increments, then prints)
[Link]("Unary minus: " + (-a)); // -12
}
}
• 6. Bitwise Operators
• Bitwise operators are used to performing the manipulation of
individual bits of a number.
• Bitwise OR Operator
• The bitwise OR | operator returns 1 if at least one of the
operands is 1. Otherwise, it returns 0.
• Let's look at the bitwise OR operation of two integers 12 and
25.
• Bitwise AND Operator
• The bitwise AND & operator returns 1 if and only if both the
operands are 1. Otherwise, it returns 0.
• Bitwise XOR Operator
• The bitwise XOR ^ operator returns 1 if and only if one of the
operands is 1. However, if both the operands are 0 or if both
are 1, then the result is 0.
• Bitwise Complement Operator
• The bitwise complement operator is a unary operator (works with only
one operand). It is denoted by ~.
• It changes binary digits 1 to 0 and 0 to 1.
• It is important to note that the bitwise complement of any integer N is
equal to - (N + 1). For example,
• Consider an integer 35. As per the rule, the bitwise complement
of 35 should be -(35 + 1) = -36. Now let's see if we get the correct answer
or not.
• In the above example, we get that the bitwise complement
of 00100011 (35) is 11011100. Here, if we convert the result into decimal
we get 220.
• However, it is important to note that we cannot directly
convert the result into decimal and get the desired output.
This is because the binary result 11011100 is also equivalent
to -36. Let's understand it -
• The result 11011100 is a signed 8-bit binary number. The
leftmost bit (1) indicates that it's a negative number (in the
case of an 8-bit signed integer, the leftmost bit determines
whether the number is positive or negative).
• To find out what negative number 11011100 represents, you
need to convert it to its positive equivalent by following the
2's complement process:
• Invert the bits of 11011100: 00100011
• Add 1: 00100011 + 1 = 00100100 (which is 36 in decimal).
• Since the original leftmost bit indicated that it's negative, the
number is -36.
• Example –
class Main {
public static void main(String[] args) {
int number = 35, result;
// bitwise complement of 35
result = ~number;
[Link](result); // prints -36
}
}
• Left Shift Operator
• The left shift operator shifts all bits towards the left by a certain
number of specified bits. It is denoted by <<.
• As we can see from the image above, we have a 4-digit number.
When we perform a 1 bit left shift operation on it, each
individual bit is shifted to the left by 1 bit.
• As a result, the left-most bit (most-significant) is discarded and
the right-most position(least-significant) remains vacant. This
vacancy is filled with 0s.
• Example –
class Main {
public static void main(String[] args) {
int number = 2;
// 2 bit left shift operation
int result = number << 2;
[Link](result); // prints 8
}
}
• Signed Right Shift Operator
• The signed right shift operator shifts all bits towards the right
by a certain number of specified bits. It is denoted by >>.
• When we shift any number to the right, the least significant
bits (rightmost) are discarded and the most significant
position (leftmost) is filled with the sign bit. For example,
• Here, we are performing the right shift of 8 (i.e. sign is
positive). Hence, there no sign bit. So the leftmost bits are
filled with 0 (represents positive sign).
• Here, we have used the signed bit 1 to fill the leftmost bits.
• The result will be -2 (by 2’s complement of 1110)
• Example –
class Main {
public static void main(String[] args) {
int number1 = 8;
int number2 = -8;
// 2 bit signed right shift
[Link](number1 >> 2); // prints 2
[Link](number2 >> 2); // prints -2
}
}
• Unsigned Right Shift Operator
• Java also provides an unsigned right shift. It is denoted by >>>.
• Here, the vacant leftmost position is filled with 0 instead of
the sign bit. For example,
• Example –
class Main {
public static void main(String[] args) {
int number1 = 8;
int number2 = -8;
// 2 bit signed right shift
[Link](number1 >>> 2); // prints 2
[Link](number2 >>> 2); // prints 1073741822
}
}
• 7. Ternary Operator
• Shorthand for an if-else statement.
• Example –
public class TernaryExample {
public static void main(String[] args) {
int a = 10, b = 20;
int max = (a > b) ? a : b;
[Link]("Maximum: " + max); // 20
}
}
• 8 - Instanceof Operator
• Used to check if an object is an instance of a particular class.
• Example –
String str = "Hello";
[Link](str instanceof String); // true