Java 27 and 26: runnable demos and captured output for every JEP, plus version lanes
Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B38FGKKam5SCGgwgduVAh3
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyStore;
|
||||
import java.time.Instant;
|
||||
import java.util.Set;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
/**
|
||||
* Small additions to the JDK 27 public API found by diffing javap dumps of 26 and 27
|
||||
* (docs/output/51-api-diff-26-to-27.txt), each exercised once. Compile with --release 27.
|
||||
*/
|
||||
public class Api27 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.printf("Math.acosh(2) = %.6f%n", Math.acosh(2));
|
||||
System.out.printf("Math.asinh(1) = %.6f%n", Math.asinh(1));
|
||||
System.out.printf("Math.atanh(.5) = %.6f%n", Math.atanh(0.5));
|
||||
Check.that(Math.abs(Math.acosh(Math.cosh(1.5)) - 1.5) < 1e-12, "acosh inverts cosh");
|
||||
|
||||
String s = "na\u00efve caf\u00e9";
|
||||
int utf8 = s.encodedLength(StandardCharsets.UTF_8);
|
||||
System.out.println("\"" + s + "\".length() = " + s.length() + ", encodedLength(UTF_8) = " + utf8);
|
||||
Check.that(utf8 == s.getBytes(StandardCharsets.UTF_8).length, "encodedLength matches getBytes().length without allocating the array");
|
||||
|
||||
BigDecimal cube = new BigDecimal("27").rootn(3, MathContext.DECIMAL64);
|
||||
System.out.println("BigDecimal(27).rootn(3) = " + cube);
|
||||
Check.that(cube.compareTo(new BigDecimal("3")) == 0, "rootn(3) of 27 is 3");
|
||||
|
||||
KeyStore ks = KeyStore.getInstance("PKCS12");
|
||||
ks.load(null, null);
|
||||
char[] pw = "changeit".toCharArray();
|
||||
ks.setEntry("aes-key", new KeyStore.SecretKeyEntry(new SecretKeySpec(new byte[16], "AES")),
|
||||
new KeyStore.PasswordProtection(pw));
|
||||
Instant created = ks.getCreationInstant("aes-key");
|
||||
System.out.println("KeyStore.getCreationInstant(\"aes-key\") returns an Instant: " + (created != null));
|
||||
Check.that(created != null && Math.abs(created.toEpochMilli() - System.currentTimeMillis()) < 60_000,
|
||||
"KeyStore.getCreationInstant returns the entry's creation time as a java.time.Instant");
|
||||
|
||||
Set<Integer> evens = Set.ofLazy(Set.of(1, 2, 3, 4), n -> n % 2 == 0);
|
||||
Check.that(evens.contains(2) && !evens.contains(3), "Set.ofLazy(Set, Predicate) exists (preview API)");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Tiny assertion helper so every demo is self-checking: a claim that stops being true turns the
|
||||
* run red instead of quietly printing something different. Passing checks are echoed, so the
|
||||
* transcript shows exactly what was verified.
|
||||
*/
|
||||
final class Check {
|
||||
private Check() {}
|
||||
|
||||
static void that(boolean condition, String claim) {
|
||||
if (!condition) {
|
||||
throw new AssertionError("CHECK FAILED: " + claim);
|
||||
}
|
||||
System.out.println("CHECK ok : " + claim);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* A behaviour change from the JDK 27 release notes (JDK-8355522) that needs no new API to trigger, so the
|
||||
* same class file can run on 26 and 27 and show the difference. Run by scripts/other-changes.sh.
|
||||
*/
|
||||
public class Compat {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("java.version = " + System.getProperty("java.version"));
|
||||
System.out.println("Locale(\"iw\").language = " + Locale.forLanguageTag("iw").getLanguage()
|
||||
+ " (Hebrew; old code 'iw', current code 'he')");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
/** Starts one child process. JDK 27 removed the VFORK launch mechanism, so a script that pinned it now fails here. */
|
||||
public class SpawnProbe {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Process p = new ProcessBuilder("true").start();
|
||||
System.out.println("child exited with " + p.waitFor());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user