JDK 26 JEP 以外の変更点ピックアップ

blog1.mammb.com

JDK 26 JEP 以外の変更点をいくつかピックアップして紹介します。


Unicode 17.0のサポート

Update Unicode Data Files to 17.0.0


JavaDoc にダークモード追加

Add dark mode for docs


Comparator.min/max の追加

Stream.min/max と Collections.min/max に加え、新たに Comparator.min/max が追加された。

Provide default methods min(T, T) and max(T, T) in Comparator interface

public interface Comparator<T> {

    default <U extends T> U max(U o1, U o2) {
        return compare(o1, o2) >= 0 ? o1 : o2;
    }
    default <U extends T> U min(U o1, U o2) {
        return compare(o1, o2) <= 0 ? o1 : o2;
    }

}


java.lang.Process が AutoCloseable を実装した

ProcessBuilder によって起動される Process が Closeable を実装し、try-with-resources ステートメントで利用可能となった

[process] java.lang.Process should implement Closeable

public abstract class Process implements Closeable {
    @Override
    public void close() throws IOException {
        // ....
    }
}


java.nio.ByteOrder の enum 化

Convert java.nio.ByteOrder to an enum

以下のようなクラスであった ByteOrder が、

public final class ByteOrder {

    private final String name;

    private ByteOrder(String name) {
        this.name = name;
    }

    public static final ByteOrder BIG_ENDIAN = new ByteOrder("BIG_ENDIAN");
    public static final ByteOrder LITTLE_ENDIAN = new ByteOrder("LITTLE_ENDIAN");
}

以下のように enum 化された。

public enum ByteOrder  {
    LITTLE_ENDIAN,
    BIG_ENDIAN;
}


HttpClient の GET リクエストで Content-Length ヘッダ送信の変更

HttpClient adds Content-Length: 0 for a GET request with a BodyPublishers.noBody()

POST または PUT 以外の HTTP/1.1 リクエストにおいて、BodyPublisher#contentLength() が 0バイトを返す場合に Content-Length ヘッダーの送信が行われなくなった。

Content-Length ヘッダーが必要な場合は、HttpRequestBuilder で明示的に追加するか、 システムプロパティ jdk.httpclient.allowRestrictedHeaders を設定する必要がある。


BodyPublishers.ofFileChannel() メソッドの追加

HttpClient: Add a BodyPublishers.ofFileChannel method

HttpRequest.BodyPublishers#ofFileChannel(FileChannel channel, long offset, long length) メソッドが追加された。

public abstract class HttpRequest {
    public static class BodyPublishers {

        public static BodyPublisher ofFileChannel(FileChannel channel, long offset, long length) throws IOException {
            Objects.requireNonNull(channel, "channel");
            return new RequestPublishers.FileChannelPublisher(channel, offset, length);
        }

    }
}

これにより、ファイルの特定の領域をアップロードすることができる。 クラウドストレージなどへの複数パートアップロードに利用できる。


CookieStore がイミュータブルなリストを返却するよう実装を変更

CookieStore.getURIs() and get(URI) should return an immutable List

CookieStore の以下のメソッドの実装がイミュータブルなリストを返却するようになった(従来はArrayList)。

public interface CookieStore {
    public List<HttpCookie> get(URI uri);
    public List<URI> getURIs();
}


Duration に MIN/MAX 定数追加

Add java.time.Duration constants MIN and MAX

最大 最小を表す定数が追加された。

public final class Duration
        implements TemporalAmount, Comparable<Duration>, Serializable {
    public static final Duration MIN = new Duration(Long.MIN_VALUE, 0);
    public static final Duration MAX = new Duration(Long.MAX_VALUE, 999_999_999);
}


Instant に plusSaturating(Duration) メソッド追加

Add a method that performs saturating addition of a Duration to an Instant

Instant#plusSaturating(Duration) が追加された。

public final class Instant
        implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable {

    public Instant plusSaturating(Duration duration) {
        if (duration.isNegative()) {
            return until(Instant.MIN).compareTo(duration) >= 0 ? Instant.MIN : plus(duration);
        } else {
            return until(Instant.MAX).compareTo(duration) <= 0 ? Instant.MAX : plus(duration);
        }
    }
}

このメソッドは、オーバーフローを起こしたり Instant の範囲を超えたりすることはなく、最も近い境界値(Instant.MIN または Instant.MAX)を返す。


MemoryMXBean に getTotalGcCpuTime() メソッド追加

JMX: Add an MXBeans method to query GC CPU time

GC 関連のアクティビティの累積 CPU 時間をナノ秒単位で返す java.lang.management.MemoryMXBean#getTotalGcCpuTime() が追加された。

public interface MemoryMXBean extends PlatformManagedObject {
    default public long getTotalGcCpuTime() {
        return -1;
    }
}

com.sun.management.OperatingSystemMXBeanProcessCpuTime と合わせて利用する。


GC CPU 時間統計出力

Refactor and make accumulated GC CPU time code generic

-Xlog:cpu オプションを付けることで、VM終了時に以下のような統計を出力する。

[62.719s][info][cpu] === CPU time Statistics =============================================================
[62.719s][info][cpu] CPUs
[62.719s][info][cpu] s % utilized
[62.719s][info][cpu] Process
[62.719s][info][cpu] Total 410.2789 100.00 6.5
[62.719s][info][cpu] Garbage Collection 124.8134 30.42 2.0
[62.719s][info][cpu] GC Threads 124.5082 30.35 2.0
[62.719s][info][cpu] VM Thread 0.3052 0.07 0.0
[62.719s][info][cpu] =====================================================================================

上記の例では、JVMプロセスは410.3秒のCPU時間を消費し、平均で6.5個のCPUコアが使用され、 そのうち、124.8秒、つまり全体の約30%がガベージコレクションに費やされたことを報告している。


InitialRAMPercentage のデフォルト値削除

Remove the default value of InitialRAMPercentage

-Xms または -XX:InitialHeapSize を指定しない場合、JVMは InitialRAMPercentage 設定に基づいて初期 Javaヒープサイズを決定する。

InitialRAMPercentage のデフォルト値は、システムの物理 RAM の 1/64(1.5625) であった。 これは不要に大きなヒープ割り当てとなる可能性があるため、デフォルト値が削除され、ヒープサイズは MinHeapSize の値に設定されるようになった。

InitialRAMPercentage を元の値である1.5625 に明示的に設定することで従来動作となる。