Java 18 delivers practical developer tools and performance improvements with a built-in web server for prototyping, enhanced documentation capabilities, and advanced APIs for high-performance computing. This release focuses on developer productivity, improved internationalization with UTF-8 as default, and continued development of the Vector API for SIMD operations.
March 2022
Java 18 delivers practical developer tools and performance improvements with a built-in web server for prototyping, enhanced documentation capabilities, and advanced APIs for high-performance computing. This release focuses on developer productivity, improved internationalization with UTF-8 as default, and continued development of the Vector API for SIMD operations.
Java 18 introduces a built-in HTTP server that provides a lightweight, easy-to-use web server for prototyping, testing, and development purposes. This feature eliminates the need for external web servers during development, making it easier to test web applications and APIs directly from the command line or within Java applications.
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.net.InetSocketAddress;
import java.io.IOException;
import java.io.OutputStream;
// Create and start simple HTTP server
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
String response = "Hello from Java 18 Web Server!";
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
});
server.createContext("/api/users", exchange -> {
String method = exchange.getRequestMethod();
String response = switch (method) {
case "GET" -> "Getting users...";
case "POST" -> "Creating user...";
default -> "Method not supported";
};
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
});
server.setExecutor(null); // Use default executor
server.start();
// Command line alternative:
// jwebserver -p 8080 -d /path/to/directoryJava 18 continues development of the Vector API as an incubator feature, providing SIMD (Single Instruction, Multiple Data) operations for high-performance computing. This API enables developers to write vectorized code that can leverage CPU vector instructions, significantly improving performance for mathematical computations, data processing, and scientific applications.
import jdk.incubator.vector.*;
// Vector operations for high-performance computing
public class VectorExample {
static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED;
public static void vectorMultiply(float[] a, float[] b, float[] c) {
int i = 0;
int upperBound = SPECIES.loopBound(a.length);
for (; i < upperBound; i += SPECIES.length()) {
FloatVector va = FloatVector.fromArray(SPECIES, a, i);
FloatVector vb = FloatVector.fromArray(SPECIES, b, i);
FloatVector vc = va.mul(vb);
vc.intoArray(c, i);
}
// Handle remaining elements
for (; i < a.length; i++) {
c[i] = a[i] * b[i];
}
}
public static float vectorDotProduct(float[] a, float[] b) {
FloatVector sum = FloatVector.zero(SPECIES);
int i = 0;
int upperBound = SPECIES.loopBound(a.length);
for (; i < upperBound; i += SPECIES.length()) {
FloatVector va = FloatVector.fromArray(SPECIES, a, i);
FloatVector vb = FloatVector.fromArray(SPECIES, b, i);
sum = sum.add(va.mul(vb));
}
float result = sum.reduceLanes(VectorOperators.ADD);
// Handle remaining elements
for (; i < a.length; i++) {
result += a[i] * b[i];
}
return result;
}
}Java 18 makes UTF-8 the default charset for the standard Java APIs, improving internationalization support and ensuring consistent text handling across different platforms. This change simplifies text processing for applications that work with international content and eliminates charset-related issues in cross-platform deployments.
// UTF-8 is now the default charset
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
// Default charset is now UTF-8
Charset defaultCharset = Charset.defaultCharset();
System.out.println("Default charset: " + defaultCharset); // UTF-8
// String operations now use UTF-8 by default
String text = "Hello, 世界!";
byte[] bytes = text.getBytes(); // Uses UTF-8 by default
String decoded = new String(bytes); // Uses UTF-8 by default
// Explicit UTF-8 usage (still supported)
byte[] utf8Bytes = text.getBytes(StandardCharsets.UTF_8);
String utf8Decoded = new String(utf8Bytes, StandardCharsets.UTF_8);
// File operations with UTF-8
import java.nio.file.Files;
import java.nio.file.Path;
Path file = Path.of("test.txt");
Files.writeString(file, "Hello, 世界!"); // Uses UTF-8 by default
String content = Files.readString(file); // Uses UTF-8 by default
// Benefits for international applications
String[] languages = {"English", "中文", "Español", "Français", "Deutsch"};
for (String lang : languages) {
System.out.println("Language: " + lang);
System.out.println("Bytes: " + lang.getBytes().length);
}