Operators are symbols that perform operations on operands. Java has a rich set, most of them borrowed from C.
Arithmetic and Relational Operators
The arithmetic operators are + (addition, and string concatenation), −, , / and % (remainder, which also works on real numbers in Java). The relational operators* compare two values and give a boolean: <, <=, >, >=, == (equal to) and != (not equal to). For objects such as strings, == compares references, so the equals() method must be used to compare contents.
Definition: Operator. A symbol that tells the compiler to perform a particular mathematical, relational or logical operation on one or more operands.
Logical and Assignment Operators
The logical operators combine boolean values: && (AND), || (OR) and ! (NOT). && and || are short-circuit operators: in a && b, b is not evaluated if a is false, and in a || b, b is not evaluated if a is true. The assignment operator = stores a value. The shorthand (compound) assignment operators combine an operation with assignment: +=, −=, *=, /=, %=, so x += 5 means x = x + 5, with an automatic cast to the type of x.
Increment and Decrement Operators
++ adds 1 and −− subtracts 1. In prefix form, ++x, the variable is incremented first and then used. In postfix form, x++, the old value is used first and then the variable is incremented. If x = 5, y = ++x gives y = 6 and x = 6, while y = x++ gives y = 5 and x = 6.
Conditional Operator
The conditional (ternary) operator ?: has the form condition ? value1 : value2. It gives value1 if the condition is true, else value2. For example, max = (a > b) ? a : b;.
Bitwise Operators
The bitwise operators work on the individual bits of integers: & (AND), | (OR), ^ (XOR), ~ (complement), << (left shift), >> (right shift, keeping the sign) and >>> (unsigned right shift, filling with zeros). Shifting left by n multiplies by 2ⁿ, and shifting right divides by 2ⁿ. For example, 12 & 10 is 8, 12 | 10 is 14 and 12 ^ 10 is 6. Other operators are the instanceof operator, which tests the type of an object, and the dot operator, which accesses members.
Table 2.3: Java operators
| Category | Operators | Example |
|---|---|---|
| Arithmetic | + − * / % | 17 % 5 = 2 |
| Relational | < <= > >= == != | 5 != 3 is true |
| Logical | && ∣∣ ! | !(a > b) |
| Assignment | = += −= *= /= %= | x += 2 |
| Increment / decrement | ++ −− | x++ |
| Conditional | ?: | a > b ? a : b |
| Bitwise | & ∣ ^ ~ << >> >>> | 12 & 10 = 8 |
Example 2.3: Prefix, postfix and bitwise
Problem: With a = 5 and b = 3, find x = a++ + ++b, the new a and b, and the results of a & b, a | b and a << 2 after that.
Solution:
- a++ uses 5, then a becomes 6. ++b makes b 4, then uses 4. x = 5 + 4 = 9.
- Now a = 6 (110) and b = 4 (100).
- a & b = 100 = 4; a | b = 110 = 6; a << 2 = 6 × 4 = 24.
Result: x = 9, a = 6, b = 4, a & b = 4, a | b = 6, a << 2 = 24.
public class Operators {
public static void main(String[] args) {
int a = 5, b = 3;
int x = a++ + ++b;
System.out.println("x = " + x + ", a = " + a + ", b = " + b);
System.out.println("a & b = " + (a & b) + ", a | b = " +
(a | b));
System.out.println("a ^ b = " + (a ^ b) + ", a << 2 = " +
(a << 2));
System.out.println("17 % 5 = " + 17 % 5 + ", -8 >> 1 = " +
(-8 >> 1));
int max = (a > b) ? a : b;
System.out.println("max = " + max + ", a > b && b > 0 = " +
(a > b && b > 0));
}
}
Output
x = 9, a = 6, b = 4
a & b = 4, a | b = 6
a ^ b = 2, a << 2 = 24
17 % 5 = 2, -8 >> 1 = -4
max = 6, a > b && b > 0 = true
Common mistakes
- Using = instead of == in a condition.
- Comparing strings with == instead of equals().
- Confusing & (bitwise or non-short-circuit AND) with &&.
Key takeaways
- % gives the remainder; / on ints gives an integer.
- && and || short-circuit.
- ++x increments before use; x++ after.
- Bitwise operators work on bits; << multiplies and >> divides by powers of 2.
Practice questions
- What is the difference between x++ and ++x?
- What does the conditional operator do?
- What is 12 ^ 10?
Answers to the practice questions
- x++ uses the value then increments; ++x increments then uses the value.
- It chooses between two values: condition ? value1 : value2.
- 6 (1100 XOR 1010 = 0110).
Sources
- Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit II
