# Io / Github / Biezhi / Java11 / Var

## Overview

This module is a compact Java 11 example focused on local variable type inference with `var` (JEP 286). It exists to demonstrate where `var` can be used in everyday code, including collection creation, stream handling, file I/O, enhanced for-loops, and try-with-resources.

The code does not define a reusable library or domain model. Instead, it acts as a teaching example that shows how Java infers types from the right-hand side of assignments and from resource declarations.

## Key Classes and Interfaces

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

`Example` is the only class in this package and serves as a single, self-contained demonstration entry point. Its `main` method walks through several `var` use cases and prints or touches a few values so the examples are concrete rather than abstract.

The class is intentionally simple: it imports common JDK classes (`ArrayList`, `List`, `Files`, `Paths`, `File`, and `FileInputStream`) and uses them in a way that highlights inferred types. There are no helpers, stateful fields, or cross-module dependencies.

#### [Example.main(String[] args)](java11-examples-master/src/main/java/io/github/biezhi/java11/var/Example.java:18)

This method demonstrates the main places where `var` can reduce repetition.

- `var list = new ArrayList<String>();` shows inference from a generic constructor call. The inferred type is `ArrayList<String>`.
- `var stream = list.stream();` shows inference from a method call. The inferred type is `Stream<String>`.
- `var newList = List.of("hello", "biezhi");` demonstrates local inference with an immutable list created by `List.of(...)`, and the method prints each element to standard output.
- `var path = Paths.get(fileName);` and `var bytes = Files.readAllBytes(path);` show `var` used with file-system APIs to read the contents of `./pom.xml`.
- `for (var b : bytes) { ... }` demonstrates `var` in an enhanced for-loop over a byte array.
- `try (var foo = new FileInputStream(new File(""))) { ... }` demonstrates `var` in a try-with-resources declaration.

Parameters:
- `String[] args` — accepted as the standard Java entry point parameter, but not used.

Returns:
- `void`

Side effects:
- Prints list items to standard output.
- Prints the byte-array reference string to standard output.
- Attempts to open a `FileInputStream` on an empty path, then swallows any resulting exception.

## How It Works

The module follows a straight-line demonstration flow inside `main`:

1. Create an `ArrayList<String>` using `var` so the reader can see type inference in a generic declaration.
2. Derive a stream from that list, again using `var` to avoid repeating the full stream type.
3. Build a small immutable list with `List.of(...)` and print its elements.
4. Read the bytes of `./pom.xml` with `Files.readAllBytes(Paths.get(fileName))`.
5. Iterate through the resulting byte array using `for (var b : bytes)`.
6. Open a `FileInputStream` inside a try-with-resources block using `var` for the resource variable.

The code is not intended to be robust or production-ready. For example, the `FileInputStream` is opened with an empty string path, so the example will typically throw and then ignore an exception. That looks deliberate: the goal is to show syntax, not to build a complete file-processing utility.

## Diagram

```mermaid
flowchart TD
A["Example.main"] --> B["Infer local variable types with var"]
A --> C["Create list and stream"]
A --> D["Read pom.xml bytes"]
A --> E["Use var in for-each"]
A --> F["Use var in try-with-resources"]
```

## Dependencies and Integration

This module only depends on the Java standard library.

- `java.util.ArrayList` and `java.util.List` are used to demonstrate inferred collection types.
- `java.nio.file.Paths` and `java.nio.file.Files` are used to show `var` with NIO file access.
- `java.io.File` and `java.io.FileInputStream` are used to demonstrate `var` in try-with-resources.

There are no package-level dependencies, no cross-module calls, and no framework integrations.

## Notes for Developers

- `var` in Java is still statically typed; it only removes redundant type annotations at the local-variable level.
- This example shows the supported positions for `var`: local variables, enhanced for-loop variables, and resource declarations.
- The example does not model error handling beyond a broad `catch (Exception e)` that ignores the exception. If you copy the pattern into real code, handle failures explicitly.
- The `stream` variable is created but not used afterward. That is acceptable in a teaching sample, but it would normally be removed in production code.
- The empty string passed to `new File("")` is likely intentional for demonstration purposes, but it is not a meaningful file path.

## Summary

`io.github.biezhi.java11.var` is a small Java 11 tutorial module that demonstrates how `var` works in real code. Its single `Example` class is designed to help engineers quickly understand the syntax and typical use cases for local variable type inference.
