Chapter 4: Operators
What are Operators?
Operators are special symbols that tell the computer to perform some action on one or more values (called operands).
Example: 5 + 3 → + is the operator, 5 and 3 are operands, result is 8.
Java has several categories of operators. We’ll cover them one by one.
1. Arithmetic Operators (Math Operations)
These are the ones you already know from school.
| Operator | Meaning | Example | Result | Notes |
|---|---|---|---|---|
| + | Addition | 10 + 5 | 15 | Also used for String concatenation |
| – | Subtraction | 20 – 8 | 12 | |
| * | Multiplication | 4 * 6 | 24 | |
| / | Division | 10 / 3 | 3 | Integer division → drops decimal |
| % | Modulus (Remainder) | 10 % 3 | 1 | Very useful! |
| + (unary) | Positive (rarely used) | +5 | 5 | |
| – (unary) | Negation | -7 | -7 |
Important Examples:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int a = 10; int b = 3; System.out.println(a + b); // 13 System.out.println(a - b); // 7 System.out.println(a * b); // 30 System.out.println(a / b); // 3 (integer division → 3.333... becomes 3) System.out.println(a % b); // 1 (remainder) double c = 10.0; System.out.println(c / b); // 3.3333333333333335 (floating-point division) |
String Concatenation with +
|
0 1 2 3 4 5 6 7 8 9 10 |
String first = "Webliance"; String last = " from Mumbai"; System.out.println(first + last); // Webliance from Mumbai System.out.println("Age: " + 25); // Age: 25 System.out.println(10 + 20 + " = thirty"); // 30 = thirty (left to right) |
2. Relational / Comparison Operators (True or False)
These compare two values and return a boolean (true or false).
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 3 | true |
| > | Greater than | 10 > 7 | true |
| < | Less than | 4 < 9 | true |
| >= | Greater than or equal | 10 >= 10 | true |
| <= | Less than or equal | 5 <= 3 | false |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
int age = 25; boolean isAdult = (age >= 18); // true System.out.println("Is adult? " + isAdult); String city = "Mumbai"; boolean isMumbai = (city == "Mumbai"); // true |
Warning: For Strings, never use == to compare content! Use .equals() instead.
|
0 1 2 3 4 5 6 7 8 9 |
String s1 = "Hello"; String s2 = new String("Hello"); System.out.println(s1 == s2); // false (different memory locations) System.out.println(s1.equals(s2)); // true (correct way!) |
3. Logical Operators (Combining Conditions)
Used to combine multiple boolean expressions.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| && | AND (both true) | (age >= 18) && (age <= 60) | true/false |
| || | OR (at least one true) | (isRaining) || (isWeekend) | true/false |
| ! | NOT (reverse) | !isRaining | true/false |
Short-circuit behavior (very important!):
- && → if first condition is false, second is not evaluated
- || → if first condition is true, second is not evaluated
Example:
|
0 1 2 3 4 5 6 7 8 9 |
int marks = 85; boolean pass = (marks >= 40) && (marks <= 100); // true boolean canVote = (age >= 18) || (hasSpecialPermission); // depends |
4. Assignment Operators (Putting Values into Variables)
| Operator | Meaning | Example | Same as |
|---|---|---|---|
| = | Simple assignment | x = 10; | |
| += | Add and assign | x += 5; | x = x + 5; |
| -= | Subtract and assign | x -= 3; | x = x – 3; |
| *= | Multiply and assign | x *= 2; | x = x * 2; |
| /= | Divide and assign | x /= 4; | x = x / 4; |
| %= | Modulus and assign | x %= 7; | x = x % 7; |
Handy Example:
|
0 1 2 3 4 5 6 7 8 9 |
int total = 100; total += 50; // total = 150 total *= 2; // total = 300 total -= 75; // total = 225 |
5. Increment / Decrement Operators (Very Common!)
Used to increase or decrease a value by 1.
| Operator | Meaning | Example | Effect on x |
|---|---|---|---|
| ++x | Pre-increment | y = ++x; | x increases first, then assigned |
| x++ | Post-increment | y = x++; | assigned first, then x increases |
| –x | Pre-decrement | y = –x; | x decreases first |
| x– | Post-decrement | y = x–; | assigned first, then x decreases |
Important Examples (this confuses many beginners!):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int x = 5; System.out.println(x++); // prints 5, then x becomes 6 System.out.println(x); // 6 System.out.println(++x); // prints 7 (x becomes 7 first) System.out.println(x); // 7 int a = 10; int b = a++; // b = 10, a = 11 int c = ++a; // a = 12, c = 12 |
6. Ternary Operator (Short if-else)
A one-line shortcut for simple if-else.
Syntax:
|
0 1 2 3 4 5 6 |
variable = (condition) ? valueIfTrue : valueIfFalse; |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
int age = 20; String status = (age >= 18) ? "Adult" : "Minor"; System.out.println(status); // Adult // Another example double discount = (purchase > 1000) ? 0.10 : 0.05; |
Real-life analogy: Ternary is like asking: “Is it raining?” → Yes → take umbrella : No → don’t take.
7. Bitwise Operators (Advanced – for later)
(We’ll just introduce them now – you’ll use them rarely at first)
| Operator | Meaning | Example |
|---|---|---|
| & | Bitwise AND | 5 & 3 |
|
Bitwise OR | |
| ^ | Bitwise XOR | 5 ^ 3 |
| ~ | Bitwise NOT (invert) | ~5 |
| << | Left shift | 5 << 1 |
| >> | Right shift (signed) | 5 >> 1 |
Example (optional for now):
|
0 1 2 3 4 5 6 7 8 |
int a = 5; // binary: 0101 int b = 3; // binary: 0011 System.out.println(a & b); // 0001 → 1 |
Quick Recap Table (Your Cheat Sheet)
| Category | Operators | Purpose |
|---|---|---|
| Arithmetic | + – * / % | Math calculations |
| Relational | == != > < >= <= | Comparisons → boolean |
| Logical | `&& | |
| Assignment | = += -= *= /= %= | Assign values quickly |
| Increment/Dec | ++ — (pre & post) | +1 or -1 |
| Ternary | ? : | Short if-else |
Homework for You (Try These!)
- Create a program OperatorsDemo.java
- Take two numbers (hardcode them)
- Print sum, difference, product, quotient, remainder
- Check if first number is greater than second
- Use ternary to print “Even” or “Odd” for the first number
- Play with increment/decrement:
Java01234567int x = 10;System.out.println(x++ + ++x + x--); // Guess the output!
- Write a program that calculates final price after discount using ternary.
You’re doing awesome! Operators are like the verbs of programming — now you know how to make your program do math, compare things, and make decisions.
Ready for Chapter 5: Input and Output (where you’ll start taking input from the user)? Or want me to explain any operator again with more examples? Just say the word! ☕🚀
