Java Lab Examples
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.
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.
| # | Description | Link |
|---|---|---|
| 1 | The smallest complete program: one class and the mandatory main | View |
| 2 | Numeric primitives printed with format specifiers | View |
| 3 | char and boolean, including a non-ASCII character | View |
| 4 | Implicit widening and explicit narrowing casts | View |
| 5 | Autoboxing: automatic conversion between int and Integer | View |
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.
| # | Description | Link |
|---|---|---|
| 6 | printf with an argument index and a type specifier | View |
| 7 | Building output with System.out.format | View |
| 8 | equals() versus the == reference test — the classic trap | View |
| 9 | Comparing several strings with equals(), case by case | View |
| 10 | Multi-line text blocks with a formatted() template | View |
| 11 | Reverse a string through a char array | View |
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.
| # | Description | Link |
|---|---|---|
| 12 | Declare, size, fill and read a one-dimensional array | View |
| 13 | A two-dimensional array walked with nested for-each loops | View |
| 14 | Generate an array from an IntStream range | View |
| 15 | Concatenate two int arrays into a new array | View |
| 16 | Split 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.
| # | Description | Link |
|---|---|---|
| 17 | The if/else statement with a positive and a negative path | View |
| 18 | An if/else-if ladder with several branches | View |
| 19 | The do/while loop, which runs its body at least once | View |
| 20 | The classic switch statement, fall-through included (reads from keyboard) | View |
| 21 | The switch expression with arrow labels and exhaustive cases | View |
| 22 | break and continue with a labelled outer loop | View |
| 23 | Nested loops with a pre-seeded break: prime numbers under 30 | View |
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.
| # | Description | Link |
|---|---|---|
| 24 | Declare a method and call it from main | View |
| 25 | Method overloading resolved by the compiler | View |
| 26 | Recursion with an explicit base case | View |
| 27 | Variable arity parameters (Integer...) | View |
| 28 | Reference semantics: an array element as an output parameter | View |
| 29 | A class with a constructor and private state | View |
| 30 | Abstract class, inheritance and polymorphism in one file | View |
| 31 | An anonymous implementation of an interface | View |
| 32 | An enum type compared with == | View |
| 33 | Records with a compact constructor that validates | View |
| 34 | Sealed classes and the permits hierarchy | View |
| 35 | Local variable type inference with var | View |
| 36 | The four access levels and what each one hides | View |
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.
| # | Description | Link |
|---|---|---|
| 37 | ArrayList walked with an explicit Iterator | View |
| 38 | ArrayList filled and printed with a stream | View |
| 39 | LinkedList: the same operations, a different structure | View |
| 40 | The forEach terminal operation | View |
| 41 | HashMap key/value pairs, listed by key set | View |
| 42 | HashSet: duplicates are dropped, order is not kept | View |
| 43 | A stream pipeline with filter and a method reference | View |
| 44 | An anonymous class versus a lambda for the same Runnable | View |
| 45 | A parallel stream spread over the common pool | View |
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.
| # | Description | Link |
|---|---|---|
| 46 | Catching an ArithmeticException and reading its message | View |
| 47 | Throwing a checked exception from main | View |
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.