MA-JAVA0 · Module 2
Types, variables, and boolean expressions
Topics: Primitives · boolean · Expressions · String
Overview
Session 2 — Types, variables, and boolean expressions
You will distinguish primitive values (int, double, boolean, char) from reference types such as String. You will evaluate arithmetic, watch promotion, use casts deliberately, and write boolean expressions (==, <, &&, ||, !) that later drive if and loops.
Time: about 85 minutes with calculator-style drills and compile checks.
Concepts
1. Primitives vs references (first pass)
Primitive types store their payload directly in the variable slot:
int— 32-bit signed integers (roughly ±2 billion).double— 64-bit floating point for science and graphics.boolean— onlytrueorfalse(never compare to0/1like in C).char— single 16-bit Unicode code unit; literals use single quotes'Z'.
Reference types (classes and interfaces) store a handle to an object elsewhere. String is a class: a variable of type String points at an immutable character sequence on the heap.
int count = 12;
String label = "ready"; // reference to immutable String object
Later sessions revisit == versus .equals for strings; for now remember == on references compares addresses, not text content.
2. Literals and underscores
Integer literals default to int unless you suffix with L for long. Floating literals default to double; append f for float.
Java allows underscores inside numeric literals for human grouping:
int rows = 1_048_576;
double nano = 3.3e-9;
The compiler strips underscores; they cannot appear at the start/end or next to a decimal point.
3. Arithmetic and promotion
Binary operators on only int operands produce int, even when mathematicians might expect fractions—integer division truncates toward zero:
int a = 7 / 3; // 2, not 2.333...
If either operand is double, Java promotes the other to double before the operation:
double x = 7 / 3.0; // 2.333...
Mixing types accidentally is a classic bug source; when in doubt, make one operand explicitly double.
4. Casting
A cast tells the compiler you accept responsibility for a narrowing conversion:
double pi = 3.14159;
int rough = (int) pi; // truncates toward zero → 3
Casting a large int to byte or short can silently wrap—avoid unless you understand modulo arithmetic on those widths.
5. final locals
Mark a local variable final when you intend no reassignment:
final int seatCap = 120;
This communicates intent and lets the compiler catch accidental reassignment during refactors.
6. Comments and naming hygiene
- Line comments start with
//. - Block comments use
/* ... */(avoid nesting nightmares—prefer multiple line comments). - Javadoc
/** ... */attaches documentation to declarations; IDEs render it.
Name variables with lowerCamelCase, classes with UpperCamelCase, and constants LIKE_THIS when static final.
7. Common beginner traps
- Forgetting
Lon huge integer literals that overflowint. - Comparing strings with
==instead of.equals. - Assuming
/always yields fractions—only when types demand floating division.
You will practise spotting these in the quiz before revealing answers.
Examples
Example A — promotion in one expression
public class ThermoMix {
public static void main(String[] args) {
int celsius = 22;
double ratio = 9.0 / 5.0;
double fahrenheit = celsius * ratio + 32;
System.out.println(fahrenheit); // 71.6
}
}
If you wrote 9 / 5 with both literals as int, you would get 1 before multiplying—always pause when translating formulas from paper to Java types.
Example B — integer remainder
int minutes = 137;
int hours = minutes / 60;
int leftover = minutes % 60;
System.out.println(hours + " h " + leftover + " m");
The remainder operator % follows truncated division toward zero for negatives—rare in beginner code but worth knowing exists.
Example C — char arithmetic quirk
char base = 'A';
int code = base + 1; // promoted to int, value 66
System.out.println((char) code); // prints B
char participates in arithmetic as a number; cast back when you intend a glyph.
Bridge
Types ↔ later Java ideas
-
Generics (Session 10) reuse the reference-vs-value intuition: a
List<String>manipulates references to strings, not inlined text blobs. -
Objects (Sessions 6–8) wrap state; primitives stay lean for hot loops—APIs often expose
intcounts anddoublemeasurements. -
Switch expressions (later language levels) still require coherent types—master classic
ifarithmetic first. -
Unit tests assert expected numeric tolerances using
doubleassertions with deltas—promotion awareness prevents false failures.
When integer versus floating paths feel boring, you are ready for structured data in Session 4.
Practice check — types, variables, and boolean expressions
Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.
1.What does `System.out.println(5 / 2);` print?
2.What does `System.out.println(5 / 2.0);` print?
3.Which declaration is a valid `boolean` value in Java?
4.For `int a = 3; int b = 4;`, what is `a < b && b != 3`?
5.What happens if you write `final double g = 9.8;` and later `g = 10;`?