Overview
Readings Reference ch. 1–2 · week 2
Every program, however big, is built from three abilities:
- Remembering things: variables, and the types that say what kind of thing each one holds.
- Calculating with them: operators like
+,/and==. - Choosing what to do next:
ifandswitchpick between options, and loops repeat things.
Most of this chapter is about how Java does each of those, and the surprises that come from the way computers store numbers (why 7 / 2 is 3, why "1" + 2 + 3 is "123"). The last part puts them together into classes, the blueprints from Ch 1, and looks at what’s really going on when you create an object.
The habit I'd build first is to trace code the way the computer would. Don't guess what a program does from how it looks. Go one statement at a time and write down every variable's value as it changes. Every surprise in this chapter shows up the moment you trace it.
Notes
Primitive data types
Memory is just a long row of bits. To store a number, Java has to decide how many bits to use and how to read them, and that’s what a type is. A primitive type is one of Java’s built-in, simplest kinds of value:
| Type | Size | Range / meaning | Typical use |
|---|---|---|---|
byte | 1 byte, signed | −128 to 127 | Raw data |
short | 2 bytes, signed | −32 768 to 32 767 | Small integers |
int | 4 bytes, signed | about ±2.1 billion | Default whole number |
long | 8 bytes, signed | about ±9.2 × 10¹⁸ | Very large whole numbers |
float | 4 bytes | ~7 significant digits | Decimals (needs f: 3.5f) |
double | 8 bytes | ~15 significant digits | Default decimal |
char | 2 bytes, unsigned | 0 to 65 535 (Unicode) | A single character 'A' |
boolean | — | true / false | Conditions |
Where the ranges come from. With bits there are possible patterns. A byte has 8 bits, so 256 patterns. “Signed” means half of them are used for negatives, which gives −128 to 127 (zero takes one of the non-negative slots, which is why the top is 127 and not 128). Each step up the table doubles the bits, so the range grows enormously. Go past the end of the range and the value wraps around: adding 1 to the biggest int gives the most negative one, with no error.
Why decimals are approximate. float and double store numbers in a binary version of scientific notation, with a fixed number of significant digits. Many simple decimals, like 0.1, can’t be written exactly in binary (just as can’t be written exactly in decimal), so they’re stored as the nearest value that fits. That’s why decimals are compared with a tolerance rather than == (Ch 4).
Why char is a number. A character is stored as its Unicode code number: 'A' is 65. That’s why you can do arithmetic on characters ('A' + 1 is 66) and compare them with <.
Example 2.1: Wrap-around and char arithmetic
What do these print?
byte b = 127;
b++;
System.out.println(b);
System.out.println(Integer.MAX_VALUE + 1);
System.out.println('A' + 1);
System.out.println((char) ('A' + 1));Show answerHide answer
- A
byteholds −128 to 127. Adding 1 to 127 wraps around to-128, with no error. - The same thing happens to an
int.Integer.MAX_VALUE + 1wraps to-2147483648. 'A'is the number 65, so'A' + 1is theint66.- Casting that
66back to achargivesB, since 66 is the code for'B'.
Arithmetic operators
+, -, *, /, and % (the remainder after division: 8 % 3 is 2, because ).
Integer division drops the fraction. When both operands are integers, the result is an integer too, and an integer can't hold a fraction. So
7 / 2is3, not3.5. The fraction is cut off, not rounded, so-7 / 2is-3. Make one operand adouble(7.0 / 2) and the whole calculation is done in decimals, giving3.5.
The rule behind this is that the result’s type comes from the operands’ types, not from what you’d like the answer to be. double d = 7 / 2; still gives 3.0. The division happens first, in integers, and only the finished 3 gets converted to a double.
% is handy for anything that repeats in cycles. n % 2 == 0 means n is even, and n % 10 is the last digit of n.
Example 2.2: Integer division and remainder
Evaluate each expression in Java: 17 / 5, 17 % 5, -17 / 5, -17 % 5, 17 / 5.0, and double d = 17 / 5;.
Show answerHide answer
17 / 5is3. Both operands are integers, so the fraction is cut off.17 % 5is2, because .-17 / 5is-3. The fraction is dropped toward zero, not rounded down.-17 % 5is-2. The remainder takes the sign of the left operand, and it keeps true.17 / 5.0is3.4. One operand is adouble, so the division happens in decimals.double d = 17 / 5;stores3.0. The division happens in integers first, and only the finished3is converted.
The overloaded +
+ does two different jobs depending on its operands:
- Both operands numbers → addition:
2 + 3is5 - Either operand a
String→ concatenation (joining):"Hello" + 2is"Hello2"
When there are several +s, Java does them one at a time, left to right, and each step decides its own meaning from the two values in front of it. Once a String appears, every result after that is a String, so every later + is concatenation too:
| Expression | Steps | Result |
|---|---|---|
"1" + 2 + 3 | "1" + 2 is "12", then "12" + 3 is "123" | "123" |
1 + 2 + "3" | 1 + 2 is 3 (both numbers), then 3 + "3" is "33" | "33" |
"1" + (2 + 3) | parentheses first: 5, then "1" + 5 is "15" | "15" |
Example 2.3: The + operator
What does each line print?
System.out.println("1" + 2 + 3);
System.out.println(1 + 2 + "3");
System.out.println("1" + (2 + 3));Show answerHide answer
- It starts with a String, so everything is concatenation:
123 1 + 2are both numbers, so that’s3first, and then3 + "3"is33- The parentheses go first:
2 + 3 = 5, then"1" + 5is15
Comparison operators
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
> / < | greater / less than |
>= / <= | greater / less than or equal to |
Each one asks a yes/no question and gives back a boolean, true or false. That boolean is what if statements and loops use to decide what to do.
=assigns and==compares.x = 3is an instruction ("make x be 3").x == 3is a question ("is x 3?").
Compound assignment and increment
Updating a variable based on its own value is so common that there are shortcuts:
| Operator | Example | Means |
|---|---|---|
+= | x += y | x = x + y |
-= | x -= y | x = x - y |
*= | x *= y | x = x * y |
/= | x /= y | x = x / y |
%= | x %= y | x = x % y |
++ and -- add or subtract 1. They do two things at once: they change the variable, and, as an expression, they also produce a value. Prefix and postfix differ only in which value they produce:
++iadds 1 first, then produces the new value.i++produces the old value, then adds 1.
int i = 5;
int a = ++i; // i becomes 6, a = 6
int b = i++; // b = 6, then i becomes 7On a line by itself (i++;), the produced value is thrown away, so the two forms do exactly the same thing. The difference only shows up when the ++ sits inside a bigger expression.
Example 2.4: Tracing prefix and postfix
What are a and i after this code?
int i = 5;
int a = i++ + ++i;Show answerHide answer
Trace it left to right, one piece at a time:
i++produces the old value5, thenibecomes6.++iadds 1 first (ibecomes7), then produces the new value7.- The sum is
5 + 7.
Result: a = 12 and i = 7. Code like this is legal but confusing, so in real programs keep ++ on a line of its own.
Conditionals
An if chain checks its conditions from the top down and runs the first branch whose condition is true, then skips the rest:
if (x == 3) {
System.out.println("x must be 3");
} else if (x == 2) {
System.out.println("x must be 2");
} else {
System.out.println("Wrong value");
}Because it stops at the first match, the order matters. If you check grade >= 50 before grade >= 80, an 85 matches the first test and never reaches the second. Put the most specific conditions first. The else at the end catches everything nothing else matched, and code after the whole chain runs no matter which branch ran.
Example 2.5: Order of conditions
What does each version print when grade = 85?
// Version A
if (grade >= 50) System.out.println("pass");
else if (grade >= 80) System.out.println("distinction");
// Version B
if (grade >= 80) System.out.println("distinction");
else if (grade >= 50) System.out.println("pass");Show answerHide answer
- Version A prints
pass. The first test (85 >= 50) is already true, so the chain stops and never reaches the>= 80test. That branch can never run. - Version B prints
distinction, which is what we wanted.
Put the narrowest condition first. The same decision as a ternary: String r = grade >= 80 ? "distinction" : "pass";.
Conditional (ternary) operator. A one-line if/else that produces a value, so it can go anywhere a value can:
String result = grade >= 50 ? "passed" : "failed";
// condition ? if true : if falseSwitch
switch compares one value against a list of constants and jumps to the matching case:
switch (x) {
case 3:
System.out.println("x must be 3");
break;
case 2:
System.out.println("x must be 2");
break;
default:
System.out.println("Wrong value");
break;
}The key idea is that case labels are just entry points, like bookmarks. switch jumps to the matching label and then keeps running statements downward. It doesn’t stop at the next label, because a label isn’t a wall. Only break makes it leave. So without break, it falls through into the next case’s statements even though that case didn’t match.
Fall-through is occasionally useful on purpose, to give several values the same code:
case 'a': case 'e': case 'i': case 'o': case 'u':
System.out.println("vowel");
break;default is where it jumps if no case matches. It’s optional and usually goes last.
Example 2.6: Switch without break
What prints if x = 2?
switch (x) {
case 1: System.out.println("one");
case 2: System.out.println("two");
case 3: System.out.println("three");
break;
default: System.out.println("other");
}Show answerHide answer
- It jumps to
case 2:and printstwo - There’s no
break, so it falls through into case 3 and printsthree breakends the switch, soothernever prints
Output: two, then three.
Loops
All three loops repeat a body while a condition is true. What differs is where the check happens:
| Loop | Checks condition | Runs at least once? | Use when |
|---|---|---|---|
while | Before each pass | No | You don’t know how many repeats in advance |
for | Before each pass | No | Counting a known number of times |
do-while | After each pass | Yes | The body has to run once before you can check (e.g. show a menu, then ask whether to repeat) |
while (x < 4) { ...; x = x + 1; }
for (x = 1; x < 4; x++) { ... } // init; condition; update
do { ... } while (x != 1); // note the semicolonA for loop is a while loop with its three parts collected on one line: the setup runs once, the condition is checked before each pass, and the update runs after each pass. Putting them together makes it hard to forget the update.
Counting passes. for (x = 1; x < 4; x++) runs for : three times. In general, x = a; x < b runs times, and x <= b runs one more. Off-by-one mistakes almost always come from < vs <=.
Infinite loops happen when nothing in the body ever makes the condition false, like forgetting
x = x + 1in awhile. Something inside the loop has to move it toward the exit.
Example 2.7: Loop counts
How many times does each loop run (or what’s x at the end)?
Show answerHide answer
| Code | Answer | Why |
|---|---|---|
int x=0; for (int i=0;i<5;i++) x = x+i; | x = 10 | |
int x=10; while (x>0) { print; x=x-1; } | 10 times | |
for (x=1; x<40; x++) | 39 times | |
int x=3; while (x<40) { print; x=x+1; } | 37 times | |
int x=3; while (x<40) { print; } | Infinite | x never changes |
int x=50; do { print; } while (x<40); | 1 time | do-while runs once before checking |
for (x=1;x<40;x++) for (y=1;y<40;y++) print; | 1521 times |
Classes and objects
A class describes a kind of object. It holds only variable declarations (what each object knows) and methods (what each object can do). Loose statements can’t sit in a class outside of a method, because a statement is an action, and actions happen when a method runs.
class ClassName {
type var1; // fields (instance variables)
type var2;
type method1(parameters) {
// statements
}
}An object is one instance of a class, made with new. Its variables are called instance variables because every instance gets its own copy.
Figure 2.1: A class is the template. Each object made with new gets its own copy of the fields, and a reference variable holds the address of its object.
Variables hold addresses, not objects
This is the most important idea in the chapter, and most later confusion traces back to it. A primitive variable like int x holds the number itself. A variable of a class type holds the object’s address: where in memory the object lives. That’s called a reference. The object itself lives elsewhere.
So creating an object takes two separate steps (usually written on one line):
- Declare a reference variable:
Car miniVan;makes a variable that can hold the address of aCar. It holdsnullfor now, meaning “doesn’t refer to anything yet”. No car exists. - Create the object and store its address:
miniVan = new Car();sets aside memory for a newCar, runs its constructor, and puts that memory’s address intominiVan.
Car miniVan = new Car(); // both steps at once
Figure 2.2: Declaring a reference variable only makes the variable (null). new makes the object, and the assignment stores its address.
Think of the reference as a remote control and the object as the TV. Declaring the variable gives you a remote that isn’t paired with anything. new builds a TV and pairs the remote with it. Using a null reference is like pressing buttons on an unpaired remote, and it causes a NullPointerException.
Example 2.8: References, null and aliases
What happens in each part?
Car a;
a.setSpeed(5); // part 1
Car p = new Car();
Car q = p;
q.setSpeed(80);
System.out.println(p.getSpeed()); // part 2Show answerHide answer
Car a;makes a reference variable but no object, soaisnull. Callinga.setSpeed(5)has nothing to follow, which causes aNullPointerException(and the compiler may refuse to build it becauseawas never assigned).Car q = p;copies the address, not the car. There is still only oneCarobject, and bothpandqrefer to it.q.setSpeed(80)changes that one car, so the print shows80.0.
Two references to one object are called aliases. Only new makes a new object.
Accessing an object uses the dot operator, which means “follow the reference to the object, then use this part of it”:
miniVan.speed = 100; // object.variable (works, but bad practice)
miniVan.setSpeed(50); // object.method() (preferred)Why prefer
setSpeed(50)overspeed = 100? Because a method can check the value first (no negative speeds) and the class can change how it stores speed later without breaking the code that uses it. Reaching straight into the variable bypasses both. Ch 5 shows howprivatelets a class forbid direct access completely.
Where does main go?
The program starts at main, so main is where the first objects get created. There are two options:
- Put
maininside the class itself. It works, but it mixes the code that uses the class in with the class. - Better: make a separate tester class (
CarTest.java) that only hasmain, in the same project.
public class CarTest {
public static void main(String[] args) {
Car miniVan = new Car();
Car sedan = new Car();
miniVan.setSpeed(100);
sedan.setSpeed(110);
System.out.println(miniVan.getSpeed()); // 100.0
System.out.println(sedan.getSpeed()); // 110.0
}
}The separate tester keeps Car clean and reusable: any program can use it, and Car doesn’t carry test code around. It also tests the class the way real code will use it, from the outside, through its methods.
Each object has its own copy of the instance variables, so changing the sedan’s speed doesn’t affect the minivan’s. Two news made two separate cars.
Methods
ReturnType methodName(parameter-list) {
// body
}- ReturnType: any primitive or object type, or
voidif it doesn’t hand back anything - methodName: no spaces, first word lowercase, later words capitalized
- parameter-list:
type namepairs separated by commas. When the method is called, the values passed in are copied into these.
A method runs on a particular object. When you call miniVan.setSpeed(50), the speed inside setSpeed means the minivan’s speed, and the same method called on sedan changes the sedan’s. Objects get work done by calling each other’s methods. In OOP terms they “send each other messages”, the way real things interact: a driver doesn’t reach into the engine, they press the pedal and let the car handle it.
Example 2.9: Extending the Car class
Add to Car: (1) a fuel variable, (2) a getter and setter for it, (3) refuel(amount) that adds fuel, (4) drive(speed) that uses 0.5 fuel per unit of speed. Then trace the test.
public class Car {
String licensePlate;
double speed;
double maxSpeed;
double fuel; // (1)
public double getFuel() { return fuel; } // (2)
public void setFuel(double val) { fuel = val; }
public void refuel(double amount) { // (3)
fuel = fuel + amount;
}
public void drive(double speed) { // (4)
fuel = fuel - speed * 0.5;
}
// ...setLicensePlate, setSpeed, getSpeed as before
}Car miniVan = new Car();
miniVan.setFuel(0);
miniVan.refuel(20);
System.out.println(miniVan.getFuel());
miniVan.refuel(20);
System.out.println(miniVan.getFuel());
miniVan.drive(20);
System.out.println(miniVan.getFuel());Show answerHide answer
| Statement | fuel | Prints |
|---|---|---|
setFuel(0) | 0.0 | |
refuel(20) | 20.0 | 20.0 |
refuel(20) | 40.0 | 40.0 |
drive(20) | 30.0 |
Inside
drive, the parameterspeedhides the instance variablespeed, sospeed * 0.5uses the argument (20) and not the car's stored speed. There's more on this in Ch 5.
Key syntax
Loops
for (int i = 0; i < n; i++) { } // runs n times (i = 0 … n-1)
while (condition) { }
do { } while (condition); // runs at least onceLoop iteration counts for for (x = a; x < b; x++):
- : starting value of the counter
- : the bound in the condition
Nested loops multiply. An outer loop with passes around an inner loop with passes runs the inner body times.
Ternary
result = condition ? valueIfTrue : valueIfFalse;Common mistakes
- Expecting
7 / 2to be3.5(integer division gives3) - Using
=instead of==in a condition - Forgetting
breakin aswitchcase and getting fall-through - Forgetting to update the loop variable, which gives an infinite loop
- Off-by-one loop counts.
i < nruns times from 0, buti <= nruns - Thinking a
do-whileskips the body when the condition starts out false (it always runs once) - Writing statements straight in a class body instead of inside a method