From 000496a35e664b48a4a3f2d39ecb9c4ffcbb9ddb Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 27 Oct 2025 20:25:39 +0000 Subject: [PATCH 1/4] Upgrade trail src code to Java 21 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes made: - English.java: Updated comments to reflect Java 21 and better document features - Letters.java: Replaced instanceof if statement with switch pattern matching, added multiple type cases and null handling - Main.java: Enhanced with Java 21 pattern matching switch, null handling, and guarded patterns with 'when' clause All files now fully leverage Java 21 features including: - Pattern matching in switch expressions - Null case handling in switch - Guarded patterns with when clauses - Enhanced type pattern matching 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- trail/src/English.java | 6 ++++-- trail/src/Letters.java | 10 +++++++--- trail/src/Main.java | 27 ++++++++++++++++----------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/trail/src/English.java b/trail/src/English.java index c7c12cc..2369e94 100644 --- a/trail/src/English.java +++ b/trail/src/English.java @@ -1,6 +1,8 @@ /** - * Simple example for using text blocks (Java 15) + - * Inheritance of sealed class + Implementation of sealed interface. + * Java 21 example demonstrating: + * - Text blocks (Java 15+) + * - Sealed class inheritance (Java 17+) + * - Sealed interface implementation (Java 17+) */ public final class English extends Letters implements Hash { @Override diff --git a/trail/src/Letters.java b/trail/src/Letters.java index 93e28e8..7322ce0 100644 --- a/trail/src/Letters.java +++ b/trail/src/Letters.java @@ -1,16 +1,20 @@ /** * Sealed class. Allows us to control the inheritance. + * Demonstrates Java 21 pattern matching in switch expressions. */ public sealed class Letters permits English { /** - * This method if statement can be replaced with a switch. + * Pattern matching with switch expression (Java 21). * * @param object to be checked for its type */ void check(Object object) { - if (object instanceof String name) { - System.out.println(name); + switch (object) { + case String name -> System.out.println("String: " + name); + case Integer num -> System.out.println("Integer: " + num); + case null -> System.out.println("Null value"); + default -> System.out.println("Unknown type"); } } } diff --git a/trail/src/Main.java b/trail/src/Main.java index d4ef9af..975c793 100644 --- a/trail/src/Main.java +++ b/trail/src/Main.java @@ -1,11 +1,17 @@ /** - * Java 17 and the new features. + * Java 21 features demonstration. + * + * Features demonstrated: + * - var keyword (Java 10+) + * - Switch expressions (Java 14+) + * - Pattern matching in switch (Java 21) + * - null handling in switch (Java 21) */ public class Main { /** * Simple print to System.out. Will receive one or more parameters. - * calls 'var-args' new feature as well. + * Uses var-args feature. * * @param x one or more variables, type String */ @@ -17,18 +23,17 @@ public static void printer(String... x) { } public static void main(String[] args) { - // var was added in Java 10, can hold any data type, type inference + // var - type inference (Java 10+) var zero = 0; if (args != null && args.length > zero) { var first = args[zero]; - if (first == null) { - printer("Null", "Nothing"); - } else { - switch (first) { - case "1" -> printer("1"); - case "2" -> System.out.println("2"); - default -> System.out.println("default"); - } + // Java 21 pattern matching switch with null handling + switch (first) { + case null -> printer("Null", "Nothing"); + case "1" -> printer("1"); + case "2" -> System.out.println("2"); + case String s when s.isEmpty() -> System.out.println("Empty string"); + default -> System.out.println("default: " + first); } } } From 10ec5d2c9a59152532710228243e91148e1542e1 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 27 Oct 2025 20:27:48 +0000 Subject: [PATCH 2/4] Update README to accurately reflect Java 21 repository content MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Changed title to indicate Java 10-21 feature demonstrations - Added repository structure section mapping files to features - Enhanced feature descriptions with explanations - Added compilation requirements specifying Java 21 needed - Improved formatting and organization for better clarity 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- README.md | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6c0fb67..5ba685c 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,35 @@ -## Going over some new features of Java +# Java Features Demo (Java 10-21) -# Java 21 +This repository demonstrates modern Java features from Java 10 through Java 21. -Record patterns, Sequenced collections, Virtual threads +## Repository Structure -# Java 17 +- `trail/src/Main21.java` - Java 21 features demonstration +- `trail/src/Main.java` - Java 17 and earlier features +- `trail/src/Letters.java` - Sealed class example +- `trail/src/Hash.java` - Sealed interface example +- `trail/src/English.java` - Text blocks and sealed types -Sealed class, Sealed interface, Switch, var-args +## Features by Version -# Java 15 +### Java 21 +- **Record patterns in switch** - Pattern matching with records in switch expressions +- **Sequenced collections** - New collection interfaces with defined ordering (addFirst, addLast, reversed) +- **Virtual threads** - Lightweight threads for high-throughput concurrent applications -Text blocks +### Java 17 +- **Sealed classes** - Control which classes can extend a class +- **Sealed interfaces** - Control which classes can implement an interface +- **Switch expressions** - Enhanced switch with arrow syntax +- **Pattern matching with instanceof** - Type patterns in instanceof +- **var-args** - Variable-length argument lists -# Java 10 +### Java 15 +- **Text blocks** - Multi-line string literals with proper formatting -Type Inference (var) +### Java 10 +- **Type inference (var)** - Local variable type inference + +## Compilation Requirements + +This code requires Java 21 or later to compile and run all examples. From 1d3333d5382ce9039078dd164f5e9e847d315e0c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 27 Oct 2025 20:33:58 +0000 Subject: [PATCH 3/4] Add Java 25 features demonstration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added two new files showcasing Java 25 features: - Main25.java: Demonstrates flexible constructor bodies (JEP 482) and primitive type patterns (JEP 455) - StreamGatherers25.java: Demonstrates the Stream Gatherers API with examples of windowFixed, windowSliding, fold, and mapConcurrent Updated README.md to include: - Java 25 section in features listing - New files in repository structure - Updated title and compilation requirements 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- README.md | 15 +++- trail/src/Main25.java | 105 ++++++++++++++++++++++++++++ trail/src/StreamGatherers25.java | 113 +++++++++++++++++++++++++++++++ 3 files changed, 230 insertions(+), 3 deletions(-) create mode 100644 trail/src/Main25.java create mode 100644 trail/src/StreamGatherers25.java diff --git a/README.md b/README.md index 5ba685c..b1a3823 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ -# Java Features Demo (Java 10-21) +# Java Features Demo (Java 10-25) -This repository demonstrates modern Java features from Java 10 through Java 21. +This repository demonstrates modern Java features from Java 10 through Java 25. ## Repository Structure +- `trail/src/Main25.java` - Java 25 features demonstration +- `trail/src/StreamGatherers25.java` - Java 25 Stream Gatherers API examples - `trail/src/Main21.java` - Java 21 features demonstration - `trail/src/Main.java` - Java 17 and earlier features - `trail/src/Letters.java` - Sealed class example @@ -12,6 +14,12 @@ This repository demonstrates modern Java features from Java 10 through Java 21. ## Features by Version +### Java 25 +- **Flexible constructor bodies (JEP 482)** - Statements before super() in constructors +- **Primitive type patterns (JEP 455)** - Pattern matching with primitive types in switch and instanceof +- **Stream Gatherers** - Custom intermediate stream operations (windowFixed, windowSliding, fold, mapConcurrent) +- **Enhanced pattern matching** - Primitive patterns in switch expressions with guards + ### Java 21 - **Record patterns in switch** - Pattern matching with records in switch expressions - **Sequenced collections** - New collection interfaces with defined ordering (addFirst, addLast, reversed) @@ -32,4 +40,5 @@ This repository demonstrates modern Java features from Java 10 through Java 21. ## Compilation Requirements -This code requires Java 21 or later to compile and run all examples. +- Java 21 examples require Java 21 or later +- Java 25 examples require Java 25 or later (or a compatible early-access build) diff --git a/trail/src/Main25.java b/trail/src/Main25.java new file mode 100644 index 0000000..688b297 --- /dev/null +++ b/trail/src/Main25.java @@ -0,0 +1,105 @@ +/** + * Java 25 new features demonstration. + * + * Features: + * 1. Flexible constructor bodies (JEP 482) - statements before super() + * 2. Primitive type patterns (JEP 455) - pattern matching with primitives + * 3. Enhanced switch with primitive patterns + */ +public class Main25 { + + // Base class for flexible constructor bodies example + static class Logger { + private final String name; + + public Logger(String name) { + this.name = name; + } + + public void log(String message) { + System.out.println("[" + name + "] " + message); + } + } + + // Demonstrates flexible constructor bodies - statements before super() + static class ServiceLogger extends Logger { + private final long startTime; + + public ServiceLogger(String serviceName) { + // Java 25: Can now validate and prepare data before super() + long initTime = System.currentTimeMillis(); + String validatedName = serviceName != null ? serviceName : "UnknownService"; + + super(validatedName); // super() no longer needs to be first statement + this.startTime = initTime; + } + + public void logStartup() { + log("Service initialized at " + startTime); + } + } + + // Demonstrates primitive type patterns in switch + public static String describePrimitive(Object value) { + return switch (value) { + case int i when i > 0 -> "Positive integer: " + i; + case int i when i < 0 -> "Negative integer: " + i; + case int i -> "Zero"; + case long l -> "Long value: " + l; + case double d when d > 0.0 -> "Positive double: " + d; + case double d -> "Non-positive double: " + d; + case null -> "Null value"; + default -> "Other type: " + value.getClass().getSimpleName(); + }; + } + + // Demonstrates primitive patterns in instanceof + public static void primitiveInstanceofExample(Object obj) { + // Java 25: Pattern matching with primitive types + if (obj instanceof int i) { + System.out.println("Integer with value: " + i); + } else if (obj instanceof double d) { + System.out.println("Double with value: " + d); + } else if (obj instanceof long l) { + System.out.println("Long with value: " + l); + } else { + System.out.println("Not a recognized primitive type"); + } + } + + // Record for pattern matching example + record Temperature(double celsius) { + public String category() { + return switch (celsius) { + case double c when c < 0.0 -> "Freezing"; + case double c when c < 20.0 -> "Cold"; + case double c when c < 30.0 -> "Moderate"; + case double c -> "Hot"; + }; + } + } + + public static void main(String[] args) { + // Flexible constructor bodies demo + System.out.println("=== Flexible Constructor Bodies ==="); + ServiceLogger logger = new ServiceLogger("PaymentService"); + logger.logStartup(); + + // Primitive type patterns in switch + System.out.println("\n=== Primitive Type Patterns in Switch ==="); + System.out.println(describePrimitive(42)); + System.out.println(describePrimitive(-5)); + System.out.println(describePrimitive(3.14)); + System.out.println(describePrimitive(100L)); + + // Primitive patterns in instanceof + System.out.println("\n=== Primitive Patterns in instanceof ==="); + primitiveInstanceofExample(Integer.valueOf(10)); + primitiveInstanceofExample(Double.valueOf(2.5)); + + // Pattern matching with primitives in records + System.out.println("\n=== Enhanced Pattern Matching ==="); + Temperature temp = new Temperature(25.5); + System.out.println("Temperature 25.5°C is: " + temp.category()); + } +} diff --git a/trail/src/StreamGatherers25.java b/trail/src/StreamGatherers25.java new file mode 100644 index 0000000..1b9bbd8 --- /dev/null +++ b/trail/src/StreamGatherers25.java @@ -0,0 +1,113 @@ +import java.util.List; +import java.util.stream.Gatherers; + +/** + * Java 25 Stream Gatherers demonstration. + * + * Features: + * 1. Stream Gatherers API - custom intermediate operations + * 2. Built-in gatherers: fold, mapConcurrent, windowFixed, windowSliding + */ +public class StreamGatherers25 { + + // Demonstrates windowFixed gatherer - process stream in fixed-size windows + public static void windowFixedExample() { + System.out.println("=== Fixed Window Gatherer ==="); + List numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + + // Process in windows of 3 + List> windows = numbers.stream() + .gather(Gatherers.windowFixed(3)) + .toList(); + + windows.forEach(window -> System.out.println("Window: " + window)); + } + + // Demonstrates windowSliding gatherer - overlapping windows + public static void windowSlidingExample() { + System.out.println("\n=== Sliding Window Gatherer ==="); + List numbers = List.of(1, 2, 3, 4, 5, 6, 7); + + // Sliding window of size 3 + List> windows = numbers.stream() + .gather(Gatherers.windowSliding(3)) + .toList(); + + windows.forEach(window -> System.out.println("Window: " + window)); + } + + // Demonstrates fold gatherer - stateful transformation + public static void foldExample() { + System.out.println("\n=== Fold Gatherer ==="); + List numbers = List.of(1, 2, 3, 4, 5); + + // Running sum using fold + List runningSums = numbers.stream() + .gather(Gatherers.fold(() -> 0, (sum, num) -> sum + num)) + .toList(); + + System.out.println("Running sums: " + runningSums); + } + + // Demonstrates mapConcurrent gatherer - parallel mapping + public static void mapConcurrentExample() { + System.out.println("\n=== Map Concurrent Gatherer ==="); + List urls = List.of("page1", "page2", "page3", "page4"); + + // Simulate concurrent processing + List results = urls.stream() + .gather(Gatherers.mapConcurrent(4, url -> { + // Simulate some processing + try { + Thread.sleep(100); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + return "Processed: " + url.toUpperCase(); + })) + .toList(); + + results.forEach(System.out::println); + } + + // Custom example: batch processing with windows + public static void batchProcessingExample() { + System.out.println("\n=== Batch Processing Example ==="); + List items = List.of("A", "B", "C", "D", "E", "F", "G"); + + // Process items in batches of 3 + items.stream() + .gather(Gatherers.windowFixed(3)) + .forEach(batch -> { + System.out.println("Processing batch: " + batch); + // Simulate batch processing + System.out.println(" -> Batch processed successfully"); + }); + } + + // Example: moving average calculation + public static void movingAverageExample() { + System.out.println("\n=== Moving Average Example ==="); + List prices = List.of(10.0, 12.0, 11.0, 13.0, 15.0, 14.0, 16.0); + + List movingAverages = prices.stream() + .gather(Gatherers.windowSliding(3)) + .map(window -> window.stream() + .mapToDouble(Double::doubleValue) + .average() + .orElse(0.0)) + .toList(); + + System.out.println("Prices: " + prices); + System.out.println("3-period moving averages: " + movingAverages); + } + + public static void main(String[] args) { + windowFixedExample(); + windowSlidingExample(); + foldExample(); + mapConcurrentExample(); + batchProcessingExample(); + movingAverageExample(); + } +} From 0e329a75bbbaa51f66b6062abb37d04ea2bf25cd Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 27 Oct 2025 20:37:10 +0000 Subject: [PATCH 4/4] Update .gitignore with modern Java project patterns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove outdated entries (BlueJ, J2ME) and add comprehensive coverage for: - Modern IDEs (IntelliJ IDEA, VS Code, Eclipse, NetBeans) - Build directories (out/, target/, build/, bin/, dist/) - OS-specific files (macOS, Windows, Linux) - Maven and Gradle build artifacts - Temporary and backup files 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .gitignore | 84 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index a1c2a23..48321fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,10 @@ -# Compiled class file +# Compiled class files *.class -# Log file +# Log files *.log -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # +# Package Files *.jar *.war *.nar @@ -19,5 +13,75 @@ *.tar.gz *.rar -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +# Virtual machine crash logs hs_err_pid* +replay_pid* + +# Build directories +out/ +target/ +build/ +bin/ +dist/ + +# IDE - IntelliJ IDEA +.idea/ +*.iml +*.iws +*.ipr + +# IDE - Eclipse +.classpath +.project +.settings/ +*.launch + +# IDE - VS Code +.vscode/ +*.code-workspace + +# IDE - NetBeans +nbproject/ +nbbuild/ +nbdist/ +.nb-gradle/ + +# macOS +.DS_Store +.AppleDouble +.LSOverride + +# Windows +Thumbs.db +ehthumbs.db +Desktop.ini + +# Linux +*~ + +# Temporary files +*.tmp +*.bak +*.swp +*.swo +*~.nib + +# Maven +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + +# Gradle +.gradle/ +gradle-app.setting +!gradle-wrapper.jar +!gradle-wrapper.properties + +# Application specific +*.pid