# Io / Github / Biezhi / Java11 / Var

## Overview

This module is a compact JDK 11 demonstration focused on local variable type inference with `var` (JEP 286). It exists as a teaching example: the code shows where `var` can be used for local variables, enhanced for-loops, and try-with-resources, while also showing places where the compiler still needs an explicit type or where `var` does not add much value.

The module is intentionally small and self-contained. Its only source file, [`src/main/java/io/github/biezhi/java11/var/Example.java`](src/main/java/io/github/biezhi/java11/var/Example.java), acts as a runnable sample rather than a reusable library component.

## Key Classes and Interfaces

### [`Example`](src/main/java/io/github/biezhi/java11/var/Example.java)

`Example` is the module's only class and serves as the executable entry point for the `var` demonstration. It is not modeling a domain concept; instead, it collects several small Java 11 language examples into one `main` method so engineers can see the syntax and the inferred types in context.

#### Purpose

The class demonstrates how the compiler infers local variable types from right-hand-side expressions. The comments in the source explicitly call out the inferred types for `ArrayList<String>` and `Stream<String>`, reinforcing that `var` is only for local variables and is resolved at compile time.

#### Main method

[`Example.main(String[] args)`](src/main/java/io/github/biezhi/java11/var/Example.java) is the entire implementation.

What it does, step by step:

1. Creates an `ArrayList<String>` using `var`:
   - `var list = new ArrayList<String>();`
   - The comment indicates the inferred type is `ArrayList<String>`.
2. Derives a stream from that list:
   - `var stream = list.stream();`
   - The comment indicates the inferred type is `Stream<String>`.
   - The variable is not used later; it exists to illustrate inference.
3. Creates an immutable list with `List.of(...)`:
   - `var newList = List.of("hello", "biezhi");`
   - The method prints each element with `forEach(System.out::println)`.
4. Reads a file from disk:
   - `String fileName = "./pom.xml";`
   - `var path = Paths.get(fileName);`
   - `var bytes = Files.readAllBytes(path);`
   - It then prints `"字节数组: " + bytes`, which will show the array object's identity rather than its contents.
5. Uses `var` in an enhanced for-loop:
   - `for (var b : bytes) { ... }`
   - This demonstrates that `var` can be used for loop variables as well.
   - The loop body is a placeholder (`// TODO`) and does not process the bytes.
6. Uses `var` in try-with-resources:
   - `try (var foo = new FileInputStream(new File(""))) { ... }`
   - The resource is printed if opened successfully.
   - Any exception is caught and ignored.
   - Because the file path is an empty string, this is primarily a syntax demonstration and is likely to fail at runtime unless the working directory resolves that path in an unusual way.

Parameters and return value:

- Parameters: `String[] args`, which are not used.
- Returns: `void`.
- Side effects: prints to standard output, reads `./pom.xml`, and attempts to open a file input stream.

## How It Works

This module follows a very simple flow:

1. Start in `main`.
2. Introduce `var` with generic collection and stream types.
3. Use `var` with factory methods and file APIs.
4. Show `var` in a for-each loop.
5. Show `var` in a try-with-resources statement.

The implementation is structured to highlight syntax features rather than business logic. There are no helper classes, no configuration, and no cross-module dependencies.

### Flow diagram

```mermaid
flowchart TD
    ExampleClass["Example"]
    MainMethod["main(String[] args)"]
    ArrayListCtor["new ArrayList<String>()"]
    ListOf["List.of(\"hello\", \"biezhi\")"]
    PathsGet["Paths.get(fileName)"]
    ReadAllBytes["Files.readAllBytes(path)"]
    FileInputStreamCtor["new FileInputStream(new File(\"\"))"]

    ExampleClass --> MainMethod
    MainMethod --> ArrayListCtor
    MainMethod --> ListOf
    MainMethod --> PathsGet
    MainMethod --> ReadAllBytes
    MainMethod --> FileInputStreamCtor
```

## Data Model

There is no domain data model in this module. The only values manipulated are standard JDK types:

- `ArrayList<String>` for the initial list demonstration
- `Stream<String>` from `list.stream()`
- `List<String>` from `List.of(...)`
- `Path` from `Paths.get(fileName)`
- `byte[]` from `Files.readAllBytes(path)`
- `FileInputStream` as a try-with-resources example

Because the module is instructional, these values are treated as examples of type inference rather than as a persistent model.

## Dependencies and Integration

### JDK APIs used

The class imports and uses only standard Java APIs:

- `java.util.ArrayList`
- `java.util.List`
- `java.nio.file.Files`
- `java.nio.file.Paths`
- `java.io.File`
- `java.io.FileInputStream`

There are no external dependencies and no resolved package dependencies in the indexed analysis.

### Integration role

This module does not integrate with other application modules. It appears to be part of a larger set of Java 11 feature examples, but this file stands alone and can be run independently as a console sample.

## Notes for Developers

- `var` is only used for local variables. It cannot replace field declarations, method parameters, or return types.
- The examples here depend on the initializer to make the type obvious. If the right-hand side is unclear, `var` can reduce readability.
- `stream` is created but unused. That is acceptable for a demo, but in production code it would be a dead local and should be removed.
- `Files.readAllBytes(path)` reads the entire file into memory. For large files, that would not be appropriate; this sample uses `./pom.xml`, which is small.
- `new FileInputStream(new File(""))` is intentionally awkward and may fail at runtime. It is best read as a syntax example for try-with-resources, not as production-ready file handling.
- The `for (var b : bytes)` loop shows that `var` works with array iteration, but the body is a placeholder and does not use the byte value.
- The printed `bytes` value is the array object reference, not a human-readable dump of the array contents. If you want the actual data, `Arrays.toString(bytes)` would be more informative.

## Source Reference

- [`src/main/java/io/github/biezhi/java11/var/Example.java`](src/main/java/io/github/biezhi/java11/var/Example.java)
