# Io / Github / Biezhi / Java11 / Var

## Overview

This module is a compact Java 11 example that demonstrates local variable type inference with `var` and a few related language conveniences. It appears to exist as a teaching/example package rather than a production feature: the code walks through how `var` behaves with collection creation, streams, NIO file access, enhanced `for` loops, and try-with-resources.

The main value of the module is showing where `var` fits naturally and where the inferred type is still obvious to the reader. New contributors can use it as a quick reference for Java 11 syntax and for the style of small demonstration programs in this codebase.

## Key Classes and Interfaces

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

`Example` is the only class in this package and acts as an executable demo. Its `main` method strings together several short examples to show how `var` can be used with generics, streams, file paths, byte arrays, enhanced `for`, and try-with-resources.

The class has no fields, helper methods, or external collaborators. That makes it easy to read top-to-bottom: each statement is a self-contained illustration of one inference scenario.

#### `main(String[] args)`

Source: [Example.main(String[] args)](src/main/java/io/github/biezhi/java11/var/Example.java:18)

What it does:
- Creates an `ArrayList<String>` using `var`, showing that the inferred type is the concrete collection type.
- Creates a stream from that list and stores it in `var`, demonstrating inference with stream pipelines.
- Builds an immutable list with `List.of(...)` and prints each item.
- Resolves a `Path` from `./pom.xml`, reads the file into a byte array, and prints the byte array reference.
- Iterates over the byte array using `for (var b : bytes)`.
- Opens a `FileInputStream` inside a try-with-resources block using `var`.

Parameters:
- `String[] args` is accepted because the method is an application entry point, but it is not used.

Returns:
- `void`.

Side effects:
- Prints list items and a byte-array message to standard output.
- Reads `./pom.xml` from disk.
- Attempts to open an empty-path `FileInputStream`, which is caught and ignored if it fails.

Important notes:
- The code is illustrative, not defensive. For example, the `for` loop body is a `TODO`, and the `catch` block suppresses all exceptions.
- The try-with-resources example shows that `var` can be used for resources whose type is still clear from the initializer.
- The `System.out.println("字节数组: " + bytes);` line prints the array object identity rather than the file contents; this reinforces that the sample is about syntax, not file processing.

## How It Works

The module follows a simple linear flow:

1. Create a generic `ArrayList` with `var`.
2. Derive a stream from the list and keep the inferred `Stream<String>` type.
3. Create a small immutable list and print it.
4. Resolve a file path and read the file into bytes.
5. Iterate over the bytes using inferred loop variables.
6. Demonstrate `var` in a resource declaration.

In other words, the class is a sequence of syntax examples rather than a reusable algorithm. Each example is independent, so there is no shared state to track between steps.

## Data Model

There is no domain model in this package. The examples work with standard JDK types only:

- `ArrayList<String>` and `List<String>` for collections
- `Stream<String>` for stream processing
- `Path` for file-system access
- `byte[]` for file contents
- `FileInputStream` for resource management

Because the module is about syntax rather than business data, there are no DTOs, entities, or custom value objects.

## Dependencies and Integration

This package depends only on the JDK:

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

There are no package dependencies recorded for this module, and no cross-module links were detected. The example is self-contained.

## Mermaid Relationship Diagram

```mermaid
flowchart TD
Root["io.github.biezhi.java11.var"] --> ExampleClass["Example"]
ExampleClass --> ListUsage["ArrayList and List.of examples"]
ExampleClass --> FileUsage["Path and Files.readAllBytes"]
ExampleClass --> LoopUsage["for (var b : bytes)"]
ExampleClass --> ResourceUsage["try-with-resources with var"]
```

## Notes for Developers

- This is a demo package, so clarity matters more than abstraction.
- `var` is used where the initializer makes the type obvious. The examples intentionally avoid ambiguous cases.
- The file read is hard-coded to `./pom.xml`, so the sample assumes it runs from the project root or a directory where that file exists.
- The empty `FileInputStream` path is likely deliberate as a quick try-with-resources demonstration, but it will usually throw and be swallowed by the `catch` block.
- If you extend this package, keep the examples short and focused so each one shows exactly one `var` use case.
