JDK 8:
List list = Arrays.asList("Java", "Python", "C++");
Collections.sort(list, (a, b) -> a.compareTo(b));
interface MyInterface {
default void printHello() {
System.out.println("Hello");
}
}
LocalDateTime now = LocalDateTime.now();
System.out.println("Current date and time: " + now);
ScriptEngineManager engineManager = new ScriptEngineManager();
ScriptEngine engine = engineManager.getEngineByName("nashorn");
engine.eval("print('Hello World!')");
JDK 9:
module com.example.greeting {
exports com.example.greeting;
}
jshell> int a = 10;
a ==> 10
jshell> int b = 20;
b ==> 20
jshell> a + b
$3 ==> 30
List numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
.filter(n -> n % 2 == 0)
.mapToInt(Integer::intValue)
.sum();
System.out.println("The sum of even numbers is " + sum);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://example.com"))
.build();
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
JDK 10:
var list = new ArrayList();
list.add("Java");
list.add("Python");
System.out.println(list);
static ThreadLocal counter = ThreadLocal.withInitial(() -> 0);
public static void main(String[] args) {
IntStream.range(0, 5)
.parallel()
.forEach(i -> {
counter.set(counter.get() + 1);
System.out.println("Thread " + i + ": " + counter.get());
});
}
java -XX:+UseAppCDS -XX:DumpLoadedClassList=classes.lst -XX:SharedClassListFile=classes.lst -XX:SharedArchiveFile=app-cds.jsa -cp myapp.jar com.example.MyApp
JDK 11:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://www.example.com"))
.build();
HttpResponse response =
client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
System.out.println("\u1F92C"); // 输出 emoji
java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -jar myapp.jar
public class MyClass {
public static void main(String[] args) throws InterruptedException {
FlightRecorderMXBean flightRecorder = ManagementFactory.getPlatformMXBean(FlightRecorderMXBean.class);
flightRecorder.startRecording();
// some code to monitor
Thread.sleep(1000);
flightRecorder.stopRecording();
Path path = Paths.get("recording.jfr");
Files.write(path, flightRecorder.getRecordingData());
}
}
JDK 12:
int day = 3;
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
default -> "Unknown";
};
System.out.println(dayName);
ScriptEngineManager engineManager = new ScriptEngineManager();
ScriptEngine engine = engineManager.getEngineByName("nashorn");
engine.eval("print('Hello World!')");
String str1 = " hello ";
String str2 = str1.strip(); // 去除开头和结尾的空格
System.out.println(str2);
JDK 13:
生成共享归档文件
java -XX:DumpLoadedClassList=classes.lst -XX:SharedClassListFile=classes.lst -XX:SharedArchiveFile=app-cds.jsa -cp myapp.jar com.example.MyApp
更新共享归档文件
java -Xshare:dump -XX:SharedArchiveFile=app-cds.jsa -cp myapp.jar com.example.MyApp
使用共享归档文件
java -Xshare:on -XX:SharedArchiveFile=app-cds.jsa -cp myapp.jar com.example.MyApp
java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -jar myapp.jar
String str = """
This is a
multi-line
string
""";
System.out.println(str);
JDK 14:
int day = 3;
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3, 4, 5 -> {
String s = "Wednesday or Thursday or Friday";
yield s;
}
default -> "Unknown";
};
System.out.println(dayName);
Object obj = "Hello";
if (obj instanceof String str) {
System.out.println(str.length());
}
record Person(String name, int age) {}
Person person = new Person("Alice", 30);
System.out.println(person.name());
JDK 15:
sealed interface Vehicle permits Car, Bike {}
final class Car implements Vehicle {}
final class Bike implements Vehicle {}
String jsonString = """
{
"name": "Alice",
"age": 30,
"address": {
"city": "New York",
"state": "NY"
}
}
""";
System.out.println(jsonString);
java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -jar myapp.jar
JDK 16:
record Person(String name, int age) {
private static final String DEFAULT_NAME = "Unknown";
private String address;
public Person(String name, int age, String address) {
this(name, age);
this.address = address;
}
public static String getDefaultName() {
return DEFAULT_NAME;
}
}
Person person = new Person("Alice", 30, "New York");
System.out.println(person.name());
Object obj = "Hello";
if (obj instanceof String s && s.length() > 0) {
System.out.println(s.toUpperCase());
}
import jdk.incubator.vector.*;
public class VectorDemo {
public static void main(String[] args) {
float[] a = {1, 2, 3, 4};
float[] b = {5, 6, 7, 8};
FloatVector va = FloatVector.fromArray(VectorSpecies_256.FLOAT, a, 0);
FloatVector vb = FloatVector.fromArray(VectorSpecies_256.FLOAT, b, 0);
FloatVector vc = va.add(vb);
float[] c = new float[4];
vc.intoArray(c, 0);
System.out.println(Arrays.toString(c));
}
}
JDK 17:
public abstract sealed class Shape permits Circle, Rectangle {}
final class Circle extends Shape {}
final class Rectangle extends Shape {}
public sealed interface Vehicle permits Car, Bike {}
public final class Car implements Vehicle {}
public final class Bike implements Vehicle {}
public sealed interface Drawable permits Circle, Rectangle, Car {}
int day = 3;
String dayName = switch (day) {
case 1, 2 -> "Weekday";
case 3, 4, 5 -> {
String s = "Midweek";
yield s;
}
case 6, 7 -> "Weekend";
default -> throw new IllegalStateException("Unexpected value: " + day);
};
System.out.println(dayName);
java -XX:+UseG1GC -XX:ConcGCThreads=4 -Xmx2g -jar myapp.jar
java -XX:+UseZGC -XX:HeapSize=16g -jar myapp.jar
java -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -jar myapp.jar
java -XX:+UseZGC -XX:HeapSize=16g -jar myapp.jar