Java Lab Examples

Forty-seven single-file programs, each short enough to read in a minute and complete enough to compile and run. They follow the lesson order of the track: foundations first, then text and arrays, control flow, functions and objects, collections and streams, and finally errors. Compile a file, run it, then change one line and run it again — the second run is where the learning happens.

Every example is self-contained: no build tool, no dependency, nothing but the standard library. Each file carries a header comment naming the class to run, and the tables below link each one to the code viewer so you can read it without cloning anything.

One file, one class, one main. A Java file that declares a public class must be named after that class, which would collide with the numbered demo names. Every demo therefore keeps a package-private class and is run by class name:
javac roadmap/java/demo/29_account.java
java -cp roadmap/java/demo Account

Foundations

The values Java works with, and the syntax that creates them. Start here: these files introduce the class-and-main skeleton, the primitive types, their wrapper classes, and the conversions between them.

#DescriptionLink
1The smallest complete program: one class and the mandatory mainView
2Numeric primitives printed with format specifiersView
3char and boolean, including a non-ASCII characterView
4Implicit widening and explicit narrowing castsView
5Autoboxing: automatic conversion between int and IntegerView

Strings & Text

String is a class, not a primitive, and that single fact explains most of the surprises in this group: strings are immutable, and == compares references while equals() compares content. The formatting examples show how to build readable output without string concatenation.

#DescriptionLink
6printf with an argument index and a type specifierView
7Building output with System.out.formatView
8equals() versus the == reference test — the classic trapView
9Comparing several strings with equals(), case by caseView
10Multi-line text blocks with a formatted() templateView
11Reverse a string through a char arrayView

Arrays

An array is a fixed-size block of values with an index, and it is the only container the language itself provides. These files cover the parts that trip people up: sizing before filling, the difference between an array of a primitive and an array of objects, and how a stream can build one for you.

#DescriptionLink
12Declare, size, fill and read a one-dimensional arrayView
13A two-dimensional array walked with nested for-each loopsView
14Generate an array from an IntStream rangeView
15Concatenate two int arrays into a new arrayView
16Split an integer into digits and find the extremes (reads from keyboard)View

Control Flow

Decisions and repetition. Read this group in order: the if statement, then the ladder, then the two loop forms, then the two switch forms — the classic statement and the modern expression that returns a value. The last two files show labelled loops and the nested-loop pattern behind a prime sieve.

#DescriptionLink
17The if/else statement with a positive and a negative pathView
18An if/else-if ladder with several branchesView
19The do/while loop, which runs its body at least onceView
20The classic switch statement, fall-through included (reads from keyboard)View
21The switch expression with arrow labels and exhaustive casesView
22break and continue with a labelled outer loopView
23Nested loops with a pre-seeded break: prime numbers under 30View

Functions & Objects

The largest group, and the heart of the language: methods first (parameters, overloading, recursion, varargs, call semantics), then the object model — encapsulation, inheritance, abstraction, anonymous classes, enums — and finally the modern type features: records, sealed hierarchies, var, and access control.

#DescriptionLink
24Declare a method and call it from mainView
25Method overloading resolved by the compilerView
26Recursion with an explicit base caseView
27Variable arity parameters (Integer...)View
28Reference semantics: an array element as an output parameterView
29A class with a constructor and private stateView
30Abstract class, inheritance and polymorphism in one fileView
31An anonymous implementation of an interfaceView
32An enum type compared with ==View
33Records with a compact constructor that validatesView
34Sealed classes and the permits hierarchyView
35Local variable type inference with varView
36The four access levels and what each one hidesView

Collections & Streams

The standard library containers and the functional pipeline that reads them. Each collection is shown twice where it matters — once with an explicit Iterator so you can see the mechanics, once with a stream or a forEach so you can see the modern idiom.

#DescriptionLink
37ArrayList walked with an explicit IteratorView
38ArrayList filled and printed with a streamView
39LinkedList: the same operations, a different structureView
40The forEach terminal operationView
41HashMap key/value pairs, listed by key setView
42HashSet: duplicates are dropped, order is not keptView
43A stream pipeline with filter and a method referenceView
44An anonymous class versus a lambda for the same RunnableView
45A parallel stream spread over the common poolView

Errors

Two programs about failure. The first catches an exception and recovers; the second declares a checked exception and lets the caller deal with it. Delete the try in the first one and watch the stack trace appear.

#DescriptionLink
46Catching an ArithmeticException and reading its messageView
47Throwing a checked exception from mainView
How to use these examples. Compile and run a file from the track's demo folder — javac roadmap/java/demo/01_hello_world.java then java -cp roadmap/java/demo HelloWorld — or open it in the code viewer and copy it out. Demos 16 and 20 read from standard input, so they expect you to type a number and a grade letter. Nothing here needs a build tool, which makes this folder a good place to practise without creating a project.

Larger, multi-file programs follow in Study Projects.