Object-Oriented Programming through Java · Packages, Exceptions and I/O
Packages and the Standard Library
In Object-Oriented Programming through Java because Ticket lives in a package, the classpath finds it, and java.lang, java.util and java.time are the library the desk already uses.
A package is a named folder of classes. cinema.desk.Ticket is not the same type as a Ticket in another package. The classpath is where the machine looks for those folders. java.lang is imported for you. Math, wrappers, Formatter, Random and java.time Instant are the library pieces this unit names.
- Object-Oriented Programming through Java
- Easy level
- 7 concepts
1A package names the folder the class lives in
A package is a namespace that matches a folder path. package cinema.desk; means the file sits under cinema/desk/ and the type's full name is cinema.desk.Ticket.
import cinema.desk.Ticket; lets another file write Ticket instead of the full name. Two classes named Ticket in two packages do not clash.
Figure. cinema.desk.Ticket is a path, not a decoration. The package line and the folder must match.
How it works
- A package names the folder the class lives inpackage is the first line. The folder path must match the dots.
First line of Ticket.java
package cinema.desk;
public class Ticket {
}2The classpath is where Java looks
The classpath is the list of directories and jar files the machine searches for compiled classes. If cinema.desk.Ticket is on disk but not on that list, run fails with a class-not-found error.
javac and java both need to see the same tree. Adding a library is adding its jar to that list — the JDBC lesson will do exactly that with Connector/J.
No diagram — the idea is carried by the prose or the Java listing.
| Piece | Role |
|---|---|
| directory | root of package folders |
| jar | a packed tree of .class files |
| missing entry | ClassNotFoundException at run |
3java.lang is already imported
java.lang holds Object, String, Math, System and the wrappers. You never write import java.lang.Object;.
Object is the parent every class already has. This lesson only needs that fact: the language package is the one you are already using.
No diagram — the idea is carried by the prose or the Java listing.
| Type | What the desk used it for |
|---|---|
| Object | the parent of Ticket |
| String | seat labels |
| System | System.out.println |
| Math | the next concept |
4Math is a bag of static functions
Math lives in java.lang and offers static methods: abs, max, min, pow, sqrt, round. There is no Math object to construct for ordinary desk work.
Meera's surcharge is Math.max(price, 150) so a floor fare still holds. The methods take primitives and return primitives.
Figure. Math.max(120, 150) returns 150. The methods are static on Math — there is no Math object to construct for ordinary desk work.
Static calls, no new Math()
int fare = Math.max(price, 150);
int cut = Math.min(price / 2, 80);
long rounded = Math.round(199.6);Floor fare
price is 120. What is Math.max(price, 150)?
- price = 120120
- Math.max(120, 150)150
Pro tip. max returns the larger argument. It does not change price.
5Wrappers box a primitive; autoboxing writes the box
A wrapper is an object that holds one primitive: Integer for int, Double for double, Boolean for boolean. Collections and generic types need objects, not primitives.
Autoboxing converts int to Integer when a method wants an object. Unboxing goes the other way. A null Integer unboxed to int throws — the primitive has no null.
No diagram — the idea is carried by the prose or the Java listing.
| Primitive | Wrapper |
|---|---|
| int | Integer |
| double | Double |
| boolean | Boolean |
| char | Character |
200 boxes to Integer; null cannot unbox
Integer boxed = 200;
int raw = boxed;
Integer missing = null;
// int crash = missing; throws at run time6Formatter writes text; Random draws numbers
java.util.Formatter (and String.format) fills a pattern: seat %s costs %d. The desk uses it for stubs, not for math.
java.util.Random draws numbers. nextInt(10) is 0..9. A seed makes the sequence repeatable in a test; without one, each run differs.
Figure. String.format fills %s %d with seat and price. Random.nextInt(10) is 0 inclusive to 10 exclusive. A seed makes that sequence repeatable; without one, each run differs.
How it works
- Formatter writes text; Random draws numbersFormatter/String.format fill a pattern. Random.nextInt(n) is 0 inclusive to n exclusive.
format a stub; draw 0..9
import java.util.Random;
String stub = String.format("%s %d", seat, price);
Random r = new Random();
int lucky = r.nextInt(10);7Instant is a moment; format it for humans
java.time.Instant is a point on the UTC timeline — when Meera bought the ticket, not a wall-clock in one city.
DateTimeFormatter turns an Instant (via a zone) into text a stub can print. Do not store that text as the source of truth; store the Instant and format at the edge.
Figure. Instant is the UTC moment Meera bought the ticket. DateTimeFormatter plus ZoneId.of("Asia/Kolkata") is the printed clock. Store the Instant; format at the edge.
How it works
- Instant is a moment; format it for humansInstant is the moment. A formatter plus a zone is the printed clock time.
Store Instant; format with a zone
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
Instant bought = Instant.now();
DateTimeFormatter fmt = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm")
.withZone(ZoneId.of("Asia/Kolkata"));
System.out.println(fmt.format(bought));Notes
- In Object-Oriented Programming through Java because Ticket lives in a package, the classpath finds it, and java.lang, java.util and java.time are the library the desk already uses.
- package cinema.desk; is the first line of the file. import names a type from another package. java.lang is already there.
- Wrappers box a primitive. Autoboxing writes that conversion for you. Instant is a moment on the timeline.
Exam traps & shortcuts
- A missing class at run time is often a classpath problem, not a typo in the Java you just compiled.
- int cannot be null. Integer can. Autoboxing a null Integer into int throws.
Recap
Packages are folders. Classpath finds them. java.lang is free. Wrappers box primitives. Instant is a moment.
- package + folder
- The dots and the directories must match.
- classpath
- A compiled class off the list is missing at run time.
- autoboxing
- int becomes Integer. null Integer does not become 0.
- Instant then format
- Store the moment. Print with a zone and a pattern.
Practise Packages and the Standard Library
Reading is free and needs no account. Practice, mocks and progress live in the app.
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device