Sub topic 5
Operators and Expressions
This chapter assumes you have completed the earlier chapters: you can write a small Java program with a main method, and you know how to declare variables with types like int and double. If you can run the code below, you are ready.
An operator is a symbol that does something to one or more values. An expression is a combination of values and operators that produces a new value. For example, 5 + 3 is an expression; the operator + adds two values, and the whole expression evaluates to 8.
Arithmetic Operators
These operators do math:
public class Operators {
public static void main(String[] args) {
int a = 17;
int b = 5;
System.out.println(a + b); // 22, addition
System.out.println(a - b); // 12, subtraction
System.out.println(a * b); // 85, multiplication
System.out.println(a / b); // 3, integer division
System.out.println(a % b); // 2, remainder
}
}Two of these need explanation:
- Division of two integers drops the fraction.
17 / 5is3, not3.4. If you want the decimal answer, use adoubletype. - The
%operator, called modulo, gives the remainder after division.17 % 5is2, because 17 is 3 groups of 5 with 2 left over. It is useful for checking if a number is even:x % 2 == 0means even.
double x = 17.0;
double y = 5.0;
System.out.println(x / y); // 3.4Comparison Operators
These operators compare two values and produce a boolean — a value that is either true or false:
| Operator | Meaning | Example | Result |
|---|---|---|---|
| `==` | equal to | `5 == 5` | `true` |
| `!=` | not equal to | `5 != 3` | `true` |
| `<` | less than | `3 < 2` | `false` |
| `>` | greater than | `3 > 2` | `true` |
| `<=` | less than or equal | `5 <= 5` | `true` |
| `>=` | greater than or equal | `4 >= 5` | `false` |
A common mistake: a single = assigns a value, while == compares two values. In the next chapter you will use these comparisons inside if statements to make decisions.
Logical Operators
Logical operators combine boolean values:
&&means and:true && falseisfalse. Both sides must be true.||means or:true || falseistrue. At least one side must be true.!means not: it flips the value.!trueisfalse.
int age = 25;
boolean hasTicket = true;
System.out.println(age >= 18 && hasTicket); // true
System.out.println(age < 18 || hasTicket); // true
System.out.println(!hasTicket); // falseAssignment Operators
The = operator stores a value in a variable. Java also has shortcuts that combine assignment with another operation:
int score = 10;
score += 5; // same as: score = score + 5; now 15
score -= 3; // now 12
score *= 2; // now 24
score++; // same as score += 1; now 25
score--; // now 24The ++ and -- operators add or subtract 1. You will see ++ a lot in the loops chapter, where counters go up by one each time around.
Order of Operations
When an expression has several operators, Java decides which to do first, just like in math class. Multiplication and division happen before addition and subtraction. Parentheses ( ) always come first and are the best way to make your meaning clear:
int result1 = 2 + 3 * 4; // 14, not 20
int result2 = (2 + 3) * 4; // 20Putting It Together
Here is a small program that uses several operators at once:
public class Bmi {
public static void main(String[] args) {
double weightKg = 70.0;
double heightM = 1.75;
double bmi = weightKg / (heightM * heightM);
boolean healthy = bmi >= 18.5 && bmi <= 24.9;
System.out.println("BMI: " + bmi);
System.out.println("In healthy range: " + healthy);
}
}Run it and you should see BMI: 22.857... and In healthy range: true. Notice the + operator doing double duty: with two numbers it adds, but when one side is text, it joins values into a string.
callout tip When a calculation gives an unexpected answer, check the types. Two int values divide to a whole number, and it is easy to forget. Wrapping one side in parentheses or storing the values as double usually fixes it.
Exercise
What is the value of 7 % 3, and why?
Show answer
It is 1. The % (modulo) operator gives the remainder after division: 7 divided by 3 is 2 with 1 left over.
Exercise
What is the difference between = and ==?
Show answer
= assigns (stores) a value into a variable. == compares two values and produces a boolean result.
Exercise
Predict the output of System.out.println(10 / 4); and System.out.println(10 / 4.0);
Show answer
The first prints 2 (integer division drops the fraction). The second prints 2.5, because 4.0 is a double, so the division keeps the decimal part.