Java Operators
A calculator has keys for numbers and keys for actions: plus, minus, equals. In Java, the action keys are operators: symbols that take one or more values, called operands, and produce a new value. You've already used +, * and =. This lesson puts all the operator families side by side, so you know what each symbol does and what kind of result it gives back.
The Operator Families
| Family | Operators | Result | Example |
|---|---|---|---|
| Arithmetic | + - * / % | A number | fare * 3 |
| Relational | == != < > <= >= | boolean | age >= 60 |
| Logical | && || ! | boolean | isPaid && hasSeat |
| Assignment | = += -= *= /= %= | The assigned value | total += 50 |
| Unary | ++ -- + - ! | Depends on the operand | count++ |
| Ternary | ? : | One of two values | age < 12 ? "child" : "adult" |
| Bitwise and shift | & | ^ ~ << >> >>> | A whole number | flags & 4 |
Three of these have their own lessons: "Ternary Operator", "Increment and Decrement Operators" and "Short-Circuit Evaluation".
Arithmetic and Remainder
public class Arithmetic {
public static void main(String[] args) {
int sweets = 17;
int children = 5;
System.out.println("Sum: " + (sweets + children));
System.out.println("Product: " + (sweets * children));
System.out.println("Each gets: " + (sweets / children));
System.out.println("Left over: " + (sweets % children));
}
}Sum: 22
Product: 85
Each gets: 3
Left over: 2How it works
- Line 7:
/with twoints is whole-number division: 17 sweets shared by 5 children is 3 each. - Line 8:
%is the remainder (modulus) operator: 2 sweets are left over. - The brackets matter: without them,
"Sum: " + sweets + childrenwould join the text"Sum: 17"and5, givingSum: 175.
Try it yourself: use % to check whether a number is even. What is 24 % 2, and what is 25 % 2?
Relational and Logical
Relational operators compare two values and give a boolean. Logical operators combine booleans:
public class Concession {
public static void main(String[] args) {
int age = 64;
boolean hasId = true;
boolean senior = age >= 60;
boolean getsConcession = senior && hasId;
System.out.println("Senior: " + senior);
System.out.println("Concession: " + getsConcession);
System.out.println("Not a senior: " + !senior);
}
}Senior: true
Concession: true
Not a senior: false| Operator | Means | True when |
|---|---|---|
a && b | AND | both are true |
a || b | OR | at least one is true |
!a | NOT | a is false |
Assignment Shortcuts
total += 50; means total = total + 50;. The same works for -=, *=, /= and %=. These compound assignment operators also include a hidden cast back to the variable's type, as the lesson "Type Promotion in Expressions" showed.
Bitwise and Shift, Briefly
Bitwise operators work on the individual bits of whole numbers. You'll rarely need them in everyday business code, but they appear in flags, permissions, hashing and interviews:
| Expression | Binary | Result |
|---|---|---|
6 & 3 | 110 & 011 = 010 | 2 |
6 | 3 | 110 | 011 = 111 | 7 |
6 ^ 3 | 110 ^ 011 = 101 | 5 |
5 << 1 | 101 → 1010 | 10 (doubles) |
20 >> 2 | 10100 → 101 | 5 (divides by 4) |
The lesson "Operator Precedence and Associativity" shows where they sit in the order of operations, and the algorithms lessons later in the course use them.
Common Mistakes
Dividing integers and expecting decimals. 7 / 2 is 3. Use 7 / 2.0 for 3.5.
Forgetting brackets when printing a sum. "Total: " + a + b joins text; write "Total: " + (a + b).
Using & and | instead of && and || for conditions. They compile, but they always evaluate both sides. The lesson "Short-Circuit Evaluation" shows why that matters.
Practice
- Easy: Given 1,000 rupees and items costing 75 rupees each, print how many items you can buy and how much money is left, using
/and%. - Harder: A movie ticket is free for children under 3 and for people with a free pass. Write the condition with
age,hasPassand logical operators, and test it with three different people.
Key Takeaways
Was this tutorial helpful?