An expression is anything that produces a value: 3, a + b, Math.sqrt(2). Java evaluates expressions the way you were taught to evaluate arithmetic, with one addition — the modulo operator — and one complication, which is that the same + symbol means two entirely different things depending on what sits on either side of it. Predictable output starts with knowing the order Java works in.
| Level | Operators | Direction | Note |
|---|---|---|---|
| 1 | ( ) | Innermost first | Brackets always win — use them when in doubt |
| 2 | ++ -- unary - | Right to left | Applies to a single operand |
| 3 | * / % | Left to right | % sits at the same level as multiply and divide |
| 4 | + - | Left to right | + is also String joining when a String is involved |
| 5 | = += -= *= /= %= | Right to left | Assignment happens last, once the value exists |
The part that catches people is level 3. BEDMAS teaches multiplication and division as one level evaluated left to right, and % simply joins that same level. So 10 % 4 * 2 is not 10 % 8; it is (10 % 4) * 2, which is 2 × 2 = 4. In the same way 20 / 3 / 2 is (20 / 3) / 2, which with integer division is 6 / 2 = 3, not 20 / 6.
System.out.println(3 + 4 * 2); // 11 multiply first
System.out.println((3 + 4) * 2); // 14 brackets first
System.out.println(10 % 4 * 2); // 4 (10 % 4) = 2, then 2 * 2
System.out.println(20 / 3 / 2); // 3 (20 / 3) = 6, then 6 / 2
System.out.println(2 + 3 * 4 - 6 / 2); // 11 2 + 12 - 3Compound assignment and increment
| Shorthand | Long form | Starting value | Result |
|---|---|---|---|
x += 3 | x = x + 3 | x is 5 | 8 |
x -= 2 | x = x - 2 | x is 8 | 6 |
x *= 4 | x = x * 4 | x is 6 | 24 |
x /= 5 | x = x / 5 | x is 24 | 4 (integer division) |
x++ | x = x + 1 | x is 4 | 5 |
x-- | x = x - 1 | x is 5 | 4 |
int a = 5;
int b = a++; // b is 5, a is now 6 (post-increment: use, then add)
int c = ++a; // a becomes 7, c is 7 (pre-increment: add, then use)
System.out.println(a + " " + b + " " + c); // prints: 7 5 7System.out.println("total: " + 1 + 2); // total: 12
System.out.println("total: " + (1 + 2)); // total: 3
System.out.println(1 + 2 + " is total"); // 3 is total
System.out.println('A' + 1); // 66 — no String, so char arithmetic
System.out.println("" + 'A' + 1); // A1 — the empty String forces joiningMath methods and keyboard input
| Method | What it does | Returns | Example |
|---|---|---|---|
Math.round(x) | Rounds a double to the nearest whole number; halves go up | long | Math.round(2.5) is 3; Math.round(-2.5) is −2 |
Math.sqrt(x) | Square root | double | Math.sqrt(16) is 4.0 |
Math.pow(a, b) | a raised to the power b | double | Math.pow(2, 10) is 1024.0 |
Math.abs(x) | Absolute value | same type as the argument | Math.abs(-7) is 7 |
Math.max(a, b), Math.min(a, b) | Larger or smaller of two values | same type as the arguments | Math.max(3, 9) is 9 |
Math.random() | A random double from 0.0 up to but not including 1.0 | double | (int)(Math.random() * 6) + 1 gives 1 to 6 |
| Rounding to cents | Scale up, round, scale back down | double | Math.round(v * 100) / 100.0 |
A Scanner reads what the user types. Because it lives in the java.util package rather than the core language, it has to be imported at the very top of the file, above the class. You then create one Scanner attached to System.in and call the method matching the kind of value you want: nextInt() for a whole number, nextDouble() for a decimal, next() for a single word, and nextLine() for a whole line of text.
import java.util.Scanner;
public class Order {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many items? ");
int count = sc.nextInt();
System.out.print("Price each: ");
double price = sc.nextDouble();
sc.nextLine(); // consume the leftover newline
System.out.print("Your name: ");
String name = sc.nextLine();
double total = count * price * 1.13; // 13% HST
System.out.println(name + " owes " + Math.round(total * 100) / 100.0);
}
}