See More

import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Java 25 Features Demonstration * Released: March 2025 * * Note: This sample was created to demonstrate Java 25 features. * Some features may still be in preview or finalized versions may differ slightly. * * Key Features (Expected/Preview): * 1. Primitive Types in Patterns (Preview) * 2. Stream Gatherers (Preview) * 3. Module Import Declarations (Preview) * 4. Flexible Constructor Bodies (Preview) * 5. Enhanced Pattern Matching */ public class Java25Features { public static void main(String[] args) { demonstratePrimitivePatterns(); demonstrateStreamGatherers(); demonstrateEnhancedPatternMatching(); demonstrateFlexibleConstructors(); } /** * Primitive Types in Patterns (Preview) * Allows pattern matching with primitive types */ private static void demonstratePrimitivePatterns() { System.out.println("=== Java 25: Primitive Patterns (Preview) ==="); Object[] values = {42, 3.14, (byte) 100, (short) 1000, 'A', true}; for (Object value : values) { String description = describePrimitive(value); System.out.println(value + " -> " + description); } System.out.println(); } private static String describePrimitive(Object obj) { // Note: Exact syntax may vary when feature is finalized // This shows the conceptual approach if (obj instanceof Integer i) { return "int: " + i + " (even: " + (i % 2 == 0) + ")"; } else if (obj instanceof Double d) { return "double: " + d; } else if (obj instanceof Byte b) { return "byte: " + b; } else if (obj instanceof Short s) { return "short: " + s; } else if (obj instanceof Character c) { return "char: " + c + " (digit: " + Character.isDigit(c) + ")"; } else if (obj instanceof Boolean bool) { return "boolean: " + bool; } return "unknown type"; } /** * Stream Gatherers (Preview) * New intermediate operation for custom stream transformations */ private static void demonstrateStreamGatherers() { System.out.println("=== Java 25: Stream Gatherers (Preview) ==="); // Traditional stream operations List numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // Group into batches (using traditional approach) System.out.println("Batching numbers:"); List> batches = new ArrayList<>(); List currentBatch = new ArrayList<>(); int batchSize = 3; for (Integer num : numbers) { currentBatch.add(num); if (currentBatch.size() == batchSize) { batches.add(new ArrayList<>(currentBatch)); currentBatch.clear(); } } if (!currentBatch.isEmpty()) { batches.add(currentBatch); } batches.forEach(batch -> System.out.println(" Batch: " + batch)); // Sliding window System.out.println("Sliding window (size 3):"); for (int i = 0; i <= numbers.size() - 3; i++) { List window = numbers.subList(i, i + 3); System.out.println(" Window: " + window); } System.out.println(); } /** * Enhanced Pattern Matching * Further improvements to pattern matching capabilities * Note: Requires Java 25+ (preview/expected features) */ private static void demonstrateEnhancedPatternMatching() { System.out.println("=== Java 25: Enhanced Pattern Matching ==="); System.out.println("Java 25 is expected to have further pattern matching enhancements"); System.out.println(); // Complex nested patterns (conceptual demonstration) record Address(String street, String city, String zipCode) {} record Person(String name, int age, Address address) {} record Company(String name, List employees) {} Company company = new Company("Tech Corp", List.of( new Person("Alice", 30, new Address("123 Main St", "Boston", "02101")), new Person("Bob", 25, new Address("456 Oak Ave", "Cambridge", "02139")) )); // Java 17 compatible approach if (company instanceof Company) { Company c = (Company) company; System.out.println("Company: " + c.name()); System.out.println("Employees: " + c.employees().size()); for (Person person : c.employees()) { System.out.println(" " + person.name() + " (" + person.age() + ") - " + person.address().city() + " " + person.address().zipCode()); } } System.out.println(); System.out.println("Java 25 expected syntax (requires Java 25+):"); System.out.println(" if (company instanceof Company(String name, List employees)) {"); System.out.println(" // Destructure the company directly"); System.out.println(" }"); /* // Java 25 expected syntax: if (company instanceof Company(String name, List employees)) { System.out.println("Company: " + name); System.out.println("Employees: " + employees.size()); for (Person person : employees) { if (person instanceof Person(String pName, int age, Address(String street, String city, String zip))) { System.out.println(" " + pName + " (" + age + ") - " + city + " " + zip); } } } */ System.out.println(); } /** * Flexible Constructor Bodies (Preview) * Allows statements before super/this calls */ private static void demonstrateFlexibleConstructors() { System.out.println("=== Java 25: Flexible Constructor Bodies (Preview) ==="); // Example of the concept (actual syntax may vary) class Example { private final String value; public Example(String input) { // In Java 25, you might be able to have statements before super() // to validate or process parameters if (input == null) { input = "default"; } // Currently this would need to be in the initialization this.value = input.toUpperCase(); } @Override public String toString() { return "Example[" + value + "]"; } } Example ex1 = new Example("hello"); Example ex2 = new Example(null); System.out.println("Example 1: " + ex1); System.out.println("Example 2: " + ex2); System.out.println(); } /** * Sample record for demonstration */ record DataPoint(String id, double value, long timestamp) { public DataPoint { if (value < 0) { throw new IllegalArgumentException("Value cannot be negative"); } } public boolean isRecent(long currentTime) { return (currentTime - timestamp) < 86400000; // 24 hours in ms } } }