Java can do math — but it follows its own strict rules about types, division, and order of operations. Today you'll learn to predict exactly what Java computes before you ever hit Run. Work through all six sections; each pill earns its checkmark only when the work in it is done.
Comments are notes for humans. The compiler ignores them completely — they never execute. Java has three types:
| Type | Looks like | Used for |
|---|---|---|
| Multiline | /* ... */ | Block headers, instructions that span lines |
| Single-line | // ... | Explaining one line; can sit at the end of a statement |
| Javadoc | /** ... */ | Java documentation — you'll meet it later this course |
An expression is made of values, variables, and operators, and it evaluates to a single value — and that value has a type. The operators:
| Symbol | Name | Example |
|---|---|---|
* | Multiplication | 4 * 3 → 12 |
/ | Division | 12 / 4 → 3 |
% | Modulo (remainder) | 7 % 3 → 1 |
+ | Addition | 4 + 3 → 7 |
- | Subtraction | 4 - 3 → 1 |
The type rule: int with int gives int. If even ONE value is a double, the result is a double — even 5.0 * 5.
. changes everything.Two facts that break people's programs: int division throws away the decimal part (18 / 5 is 3, not 3.6), and dividing an int by zero crashes the program with an ArithmeticException.
Enter two values and an operator. The lab shows what your calculator says next to what Java says. A value with a decimal point (like 18.0) is a double; without one it's an int.
ArithmeticException. (Then try 18.0 / 0 and see something even stranger.)a % b is the remainder after dividing a by b. Predict all four — type each answer, then check.When several operators share one expression, Java uses operator precedence — PEMDAS with one addition: *, /, and % are all the same level, evaluated left to right. Then + and -, also left to right.
Parentheses override precedence, exactly like in math.
5 + 12 * 18 - 2 / 2 % 2 (worth 220 as written) and add exactly one pair of parentheses to make it as small as possible. Can you hit 0? Type your version and test it — this is a real evaluator, it computes whatever you give it.Your mass is the same everywhere in the universe — but your weight depends on the planet's gravity. The conversion is one expression:
weightOnPlanet = weightOnEarth * planetGravity / earthGravity;
Earth's gravity is 9.81 m/s². Jupiter's is a monstrous 24.79.
Eight questions. You get one retry per question. After that, the question locks — bring it to Mr. Babb instead of guessing.
Q1. Why do you think the order of operations still applies in Java?
Q2. Why is it important to know the type of a variable?
/* */, //, /** */) are for humans; the compiler ignores them.a % b is the remainder of a divided by b: 7 % 3 is 1.