Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 14, 2025 16:36
Show Gist options
  • Select an option

  • Save up1/3912fdfc19c574fce9f114ecd61b057c to your computer and use it in GitHub Desktop.

Select an option

Save up1/3912fdfc19c574fce9f114ecd61b057c to your computer and use it in GitHub Desktop.
Java 25
# ทำการติดตั้งและ config Java 25 :: https://jdk.java.net/25/
$javac -version
javac 25
$java -version
openjdk version "25" 2025-09-16
OpenJDK Runtime Environment (build 25+35-3488)
OpenJDK 64-Bit Server VM (build 25+35-3488, mixed mode, sharing)
class Demo01 {
void main(String[] args) {
int a = 10;
if (a instanceof int i) {
System.out.println("a is an int: "+ i);
}
}
}
# Run
$javac --enable-preview --release 25 Demo01.java
$java --enable-preview Demo01
a is an int: 10
import java.util.concurrent.StructuredTaskScope;
public class DemoConcurrency {
static Integer process1() {
System.out.println("Processing in process 1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread 1 interrupted");
}
return 2000; // Simulating some processing result
}
static Integer process2() {
System.out.println("Processing in process 2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread 2 interrupted");
}
return 1000; // Simulating some processing result
}
// Main method to run the demo
void main(String[] args) {
try (var scope = StructuredTaskScope.<Integer>open()) {
var firstProcess = scope.fork(() -> process1());
var secondProcess = scope.fork(() -> process2());
scope.join();
System.out.println("Process 1 result: " + firstProcess.get());
System.out.println("Process 2 result: " + secondProcess.get());
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
# Run
$javac --enable-preview --release 25 DemoConcurrency.java
$java --enable-preview DemoConcurrency
Processing in process 1
Processing in process 2
Process 1 result: 2000
Process 2 result: 1000
class Base {
final int x;
Base(int x) {
this.x = x;
}
}
class Derived extends Base {
Derived(int x) {
if(x <10) {
throw new IllegalArgumentException("x must be at least 10");
}
super(x);
}
}
public class DemoConstructor {
void main(String[] args) {
Derived d1 = new Derived(15);
System.out.println("Derived x: " + d1.x);
Derived d2 = new Derived(5);
System.out.println("Derived x: " + d2.x);
}
}
# Run
$javac DemoConstructor.java
$java DemoConstructor
Derived x: 15
Exception in thread "main" java.lang.IllegalArgumentException: x must be at least 10
at Derived.<init>(DemoConstructor.java:11)
at DemoConstructor.main(DemoConstructor.java:21)
import module java.base;
public class DemoModule {
void main(String[] args) {
Date currentDate = new Date();
System.out.println("Current date: " + currentDate);
}
}
# Run
$javac --enable-preview --release 25 DemoModule.java
$java --enable-preview DemoModule
Current date: Thu Aug 14 22:45:28 GMT+07:00 2025
void main(String[] args) {
IO.println("Hello, Java 25!!");
}
# Run
$javac Hello.java
$java Hello
Hello, Java 25!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment