# Getting Started

## What This Project Does

This repository is a hands-on showcase of new language features and APIs introduced in Java 9 through Java 11. Each sub-package under `src/main/java/io/github/biezhi/java11/` contains a self-contained example demonstrating a specific JEP (JDK Enhancement Proposal) — from `var` local-variable type inference and the new `HttpClient`, to `String.repeat()` and single-file source-code execution. It is designed as a quick reference or learning sandbox rather than a production application.

## Recommended Reading Order

Start here if you are new to the codebase:

1. **README.md** — the top-level index of all example categories
2. **`singlefile/HelloWorld.java`** — the simplest entry point; run directly with `java HelloWorld.java` to see JEP 330 in action
3. **`var/Example.java`** — demonstrates `var` type inference, the most widely used Java 10 feature
4. **`string/Example.java`** — covers `String` helper methods added in Java 11 (`isBlank()`, `strip()`, `repeat()`, `lines()`)
5. **`http/Example.java`** — the largest example file; covers the new built-in `HttpClient` with sync and async patterns
6. **`collections/Example.java`** — shows `List.of()`, `Map.of()`, `Map.entry()`, and the diamond operator

After these, browse any remaining category at will. Each example is standalone with no inter-class dependencies.

## Key Entry Points

| File | What it demonstrates |
|------|---------------------|
| `singlefile/HelloWorld.java` | JEP 330 — run a single `.java` file without `javac` |
| `var/Example.java` | JEP 286 — local-variable type inference with `var` |
| `string/Example.java` | JEP 332/378/383 — `isBlank()`, `strip()`, `repeat()`, `lines()` |
| `http/Example.java` | JEP 321 — the new `java.net.http.HttpClient` API (sync + async) |
| `collections/Example.java` | `List.of()`, `Map.of()`, `Map.entry()` — immutable collections factory methods |
| `files/Example.java` | JEP 327 — `Files.readString()` / `Files.writeString()` |
| `interfaces/Example.java` | JEP 246 — private methods in interfaces |
| `processor/Example.java` | JEP 158 — `ProcessHandle` API for process inspection |
| `time/Example.java` | `java.time.Duration` combined with `TimeUnit` conversion |
| `trywithresources/Example.java` | JEP 144/301 — improved try-with-resources (effectively final) |

All example classes live under the package `io.github.biezhi.java11.*` and follow a consistent naming convention: a single `Example.java` (or `HelloWorld.java`) per feature area.

## Project Structure

```
pom.xml                           # Maven build (Java 11 release target)
src/main/java/io/github/biezhi/java11/
  var/        Example.java        # var type inference
  string/     Example.java        # new String methods
  collections/ Example.java       # immutable collections factories
              DiamondOperatorExample.java  # diamond operator + anonymous classes
  interfaces/ Example.java        # private interface methods
  http/       Example.java        # new HttpClient (sync/async)
              Foo.java            # POJO used in HTTP POST example
  processor/  Example.java        # ProcessHandle API
  files/      Example.java        # Files.readString/writeString
  time/       Example.java        # Duration + TimeUnit
  trywithresources/ Example.java  # try-with-resources improvement
  singlefile/ HelloWorld.java     # single-file execution
```

No module-info.java, no external framework dependencies beyond [Gson](https://github.com/google/gson) for JSON serialization in the HTTP example.

## Configuration

**`pom.xml`** controls the entire build:

| Setting | Value | Purpose |
|---------|-------|---------|
| `maven.compiler.release` | `11` | Compiles against the JDK 11 API; no separate source/target pair needed |
| `maven-surefire-plugin.argLine` | `--add-modules=jdk.incubator.httpclient` | Grants the HTTP client incubator module access at test-compile time |
| `gson` dependency | `2.8.5` | Only dependency; used for serializing POJOs in the HTTP POST example |

To build and run:

```bash
mvn compile                        # compile all examples
java -cp target/classes io.github.biezhi.java11.var.Example   # run one example
java src/main/java/io/github/biezhi/java11/singlefile/HelloWorld.java  # single-file mode
```

## Common Patterns

### One example per category

Each feature area has its own directory containing a single `Example.java`. There are no packages, no tests, no `src/test/java` — this is purely an educational catalog.

### Static `main` entry points

Every example class follows the same skeleton:

```java
public class Example {
    public static void main(String[] args) throws Exception {
        // demonstrate the feature
    }
}
```

Helper methods are `private static void` and are called sequentially from `main`. There is no dependency injection, no framework wiring.

### `var` throughout

The codebase makes heavy use of Java 10's `var` keyword for local variable inference. When scanning the code, mentally resolve `var` to its inferred type from the RHS expression.

### Builder-style request construction

The `http/Example.java` uses the `HttpRequest.Builder` fluent API, which is the canonical pattern for the new HttpClient:

```java
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://example.com"))
        .header("Content-Type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
        .build();
```

### Async with `CompletableFuture`

All async HTTP calls use `client.sendAsync(...).whenComplete(...).join()`. The `whenComplete` handler checks for null-ness of the exception parameter before accessing the response.
