Java

エンタープライズ開発で広く使われるオブジェクト指向言語で、JVM 上で動作しプラットフォーム非依存を実現する

プログラミング言語ランタイム

Java とは

Java は、Sun Microsystems (現 Oracle) が 1995 年にリリースしたオブジェクト指向言語で、JVM (Java Virtual Machine) 上で動作する。「Write Once, Run Anywhere」の理念で、コンパイルしたバイトコードがどの OS でも動作する。エンタープライズ開発、Android アプリ、ビッグデータ処理で広く使われている。

Java 21 の主要機能

Java 21 では Virtual Threads (goroutine に相当する軽量スレッド)、switch 式でのパターンマッチ (Pattern Matching)、ボイラープレートなしのイミュータブルなデータクラス (Record)、継承を制限する Sealed Classes、複数行文字列リテラル (Text Blocks) が導入された。

// Record: ボイラープレートなしのデータクラス
record User(String id, String name, String email) {}

// Virtual Threads: 数百万の軽量スレッド
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    executor.submit(() -> handleRequest(request));
}

// Pattern Matching
String describe(Object obj) {
    return switch (obj) {
        case Integer i when i > 0 -> "positive: " + i;
        case String s -> "string: " + s;
        case null -> "null";
        default -> "unknown";
    };
}

TypeScript / Go との比較

観点 Java TypeScript Go
型システム 静的 (名前的型付け) 静的 (構造的型付け) 静的 (構造的型付け)
実行環境 JVM V8 / Bun ネイティブバイナリ
並行処理 Virtual Threads async/await goroutine
起動速度 遅い (JVM 起動) 速い 速い
エコシステム Maven Central npm Go Modules
用途 エンタープライズ、Android Web アプリ CLI、サーバー

Lambda での Java

Java Lambda はコールドスタートが遅い (数秒) のが課題。SnapStart で改善できる。

public class Handler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) {
        return new APIGatewayProxyResponseEvent()
            .withStatusCode(200)
            .withBody("{\"message\": \"Hello\"}");
    }
}

Spring Boot

Java のエンタープライズ開発で最も使われるフレームワーク。DI (依存性注入)、Web MVC、データアクセスを統合する。

GraalVM Native Image

GraalVM でネイティブバイナリにコンパイルすると、起動時間が数十ミリ秒に短縮される。Lambda のコールドスタート問題を根本的に解決する。

Java を扱う関連書籍も多い。

関連用語