JEP 447: Statements before super(...) (Preview) JEP 454: Foreign Function & Memory API JEP 456: Unnamed Variables & Patterns JEP 457: Class-File API (Preview) JEP 458: Launch Multi-File Source-Code Programs JEP 459: String Templates (Second Preview) JEP 460: Vector API (Seventh Incubator) JEP 461: Stream Gatherers (Preview) JEP 462: Structured Concurrency (Second Preview) JEP 463: Implicitly Declared Classes and Instance Main Methods (Second Preview) JEP 464: Scoped Values (Second Preview) 6
Child extends Parent { public Child(String name) { super(name); // 制約による親クラスのコンストラクタ呼び出し if (name.equals("Anakin")) throw new IllegalArgumentException("you're not my father"); } } 10
{ public Name(String fullName) { var names = fullName.split(" "); // 今まではこれがNG this(names[0], names[1]); } } var a = new Name("Yuji KUBOTA"); a ==> Name[first=Yuji, last=KUBOTA] 13
this before supertype constructor has been called this.i++; // Error ^ ConstructionTest.java:8: error: cannot reference this before supertype constructor has been called this.hashCode(); // Error ^ ConstructionTest.java:9: error: cannot reference this before supertype constructor has been called System.out.print(this); // Error ^ ConstructionTest.java:10: error: cannot reference i before supertype constructor has been called i++; // Error ^ ConstructionTest.java:11: error: cannot reference hashCode() before supertype constructor has been called hashCode(); // Error ^ ConstructionTest.java:18: error: cannot reference super before supertype constructor has been called super.i++; // Error ^ ConstructionTest.java:29: error: cannot reference this before supertype constructor has been called new Inner(); // Error - 'this' is enclosing instance ^ ConstructionTest.java:36: error: cannot reference this before supertype constructor has been called Inner.this.inner++; // Error - same instance 18
x, int y)) { System.out.println(y); /* x は不要*/ } // After if (obj instanceof Point(int _, int y)) { System.out.println(y); } if (obj instanceof Point(_, int y)) { System.out.println(y); } switch (o) { case Point(int x, _) -> ... } NG // Type o instanceof _ o instanceof _(int x, int y) case _ 22
uses preview features of Java SE 22. Note: Recompile with -Xlint:preview for details. $ java --enable-preview Test Declared # 念のためクラスファイルは削除 $ java --enable-preview --source 22 Test.java Declared 60
class Hoge is public, should be declared in a file named Hoge.java public class Hoge { ^ エラー1 個 $ java --enable-preview --source 22 Test.java Hoge 流れ のセクションで説明した通り、source-file modeではファイル名で一致したクラスでは なく、最初のトップレベルクラスが優先して選ばれる。普通にコンパイルするとエラー。 63
s = x + " plus " + y + " equals " + (x + y); // 細かすぎる s = new StringBuilder() .append(x) .append(" plus ") .append(y) .append(" equals ") .append(x + y) .toString(); // 引数の数や型の不一致を招きやすい s = String.format("%2$d plus %1$d equals %3$d", x, y, x + y); s = "%2$d plus %1$d equals %3$d".formatted(x, y, x + y); // 独特すぎる MessageFormat mf = new MessageFormat("{0} plus {1} equals {2}"); s = mf.format(x, y, x + y); 64
20.5; // 変数は \{ で始まり } で閉じる String s = STR."\{ x } plus \{ y } equals \{ x + y }"; s ==> "10.0 plus 20.5 equals 30.5" 文字列変換前のデータ構造がそのまま返る RAW も用意されている StringTemplate.RAW."\{ x } plus \{ y } equals \{ x + y }"; $1 ==> StringTemplate{ fragments = [ "", " plus ", " equals ", "" ], values = [10.0, 20.5, 30.5] } // * 注 *注: 見易さのため改行を入れています 65
{ if (windowSize < 1) throw new IllegalArgumentException("'windowSize' must be greater than zero"); class FixedWindow { Object[] window; int at; FixedWindow() { at = 0; window = new Object[windowSize]; } boolean integrate(TR element, Downstream<? super List<TR>> downstream) { window[at++] = element; if (at < windowSize) { return true; } else { final var oldWindow = window; window = new Object[windowSize]; at = 0; return downstream.push( SharedSecrets.getJavaUtilCollectionAccess() .listFromTrustedArrayNullsAllowed(oldWindow) ); } } 89