Java11 API Changes

JDK11 features are frozen since it was in the Rampdown phase last month.


Big changes are listed in this page as JEPs.
JDK 11


However there are many changes outside of JPEs in JDK 11, therefore I list up the API changes in JDK 11 as far as I know.

String

lines()

Get a stream divided by line breaks.

jshell> "test\nhoge\n".lines().map(String::toUpperCase).toArray()
$11 ==> Object[2] { "TEST", "HOGE" }
repeat(int)

Repeat string for the specified times.

jshell> "test".repeat(3)
$7 ==> "testtesttest"
isBlank()

The method appended that determines whether the string contains only spaces or not. Full width space is also treated as a space.

jshell> var halfSpace = "\u0020"
halfSpace ==> " "

jshell> halfSpace.isBlank()
$11 ==> true

jshell> var fullSpace = "\u3000"
fullSpace ==> " "

jshell> fullSpace.isBlank()
$13 ==> true
strip() / stripLeading() / stripTrailing()

Almost same as trim() / trimLeft() / trimRight() but it takes full width spaces as a spece.

jshell> var aaa = fullSpace + "aaa" + fullSpace
aaa ==> " aaa "

jshell> aaa.strip()
$14 ==> "aaa"

jshell> aaa.trim()
$15 ==> " aaa "

CharSequence

compare(CharSequence, CharSequence)

Sort by dictionaly order.
It is used by compateTo in CharSequence/StringBuffer/StringBuilder. Therefore the 3 classes implements Comparable.

Character

toString(int)

It was not convenient so far but now it become convenient.
JDK10.0.1

jshell> Character.toString(65)
|  Error:
|  incompatible types: possible lossy conversion from int to char
|  Character.toString(65)
|                     ^^


JDK11ea14

jshell> Character.toString(65)
$9 ==> "A"

Path

of(String, String...)

We had to use Paths.get() so far, now we can use of() as same manner as another classes.

Files

writeString(Path, CharSequence)

We can save a string with 1 method.

jshell> Files.writeString(Path.of("test.txt"), "Hello!!!")
$3 ==> test.txt
readString(Path)

We can read a string with 1 method.

jshell> Files.readString(Path.of("test.txt"))
$4 ==> "Hello!!!"

Reader

nullReader()

We can get a Reader that do nothing.

Writer

nullWriter()

We can get a Writer that do nothing.

Predicate

not(Predicate)

We could not use a method reference where it needs to invert the condition, now we can use a method reference.

jshell> Stream.of("aa", "", "bb").filter(Predicate.not(String::isEmpty)).toArray()
$23 ==> Object[2] { "aa", "bb" }

Collection

toArray(IntFunction)

We had to use a non stylish notation like list.toArray(new String[list.size())]) on creating an typed array from a collection, now we can write in a stylish notation.

jshell> List.of("aa","bb").toArray(String[]::new)
$1 ==> String[2] { "aa", "bb" }

Optional/OptionalInt/OptionalLong/OptionalDouble

isEmpty()

isPresent() had existed so far, now we have isEmpty().

jshell> Optional.ofNullable(null).isEmpty()
$5 ==> true

TimeUnit

convert(Duration)

added for java.util.concurrent.TimeUnit

Pattern

asMatchPredicate()

There was asPredicate that do find, now we have asMatchPredicate that do match.

jshell> var pred = Pattern.compile("aaa").asPredicate()
pred ==> java.util.regex.Pattern$$Lambda$25/0x00000008000b5040@2f686d1f

jshell> pred.test("aaa")
$6 ==> true

jshell> pred.test("aaab")
$7 ==> true

jshell> var matPred = Pattern.compile("aaa").asMatchPredicate()
matP ==> java.util.regex.Pattern$$Lambda$24/0x00000008000b6440@402a079c

jshell> matPred.test("aaa")
$9 ==> true

jshell> matPred.test("aaab")
$10 ==> false

ListSelectoinModel

getSelectedIndices()/getSelectedCount() are added.
Swing is changing!

Thread

destroy() / stop(Throwable)

Removed. stop() remains.

Policy

javax.security.auth.Policy is removed.

ArrayIndexOutOfBoundsException

The message become gentle for human.
JDK10.0.1

jshell> new int[]{}[0]
|  java.lang.ArrayIndexOutOfBoundsException thrown: 0
|        at (#8:1)


JDK11-ea14

jshell> new int[]{}[0]
|  Exception java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
|        at (#4:1)

IndexOutOfBoundsException

hyphens are removed in the message.
JDK10.0.1

jshell> List.of().get(0)
|  java.lang.IndexOutOfBoundsException thrown: Index 0 out-of-bounds for length 0
|        at Preconditions.outOfBounds (Preconditions.java:64)
|        at Preconditions.outOfBoundsCheckIndex (Preconditions.java:70)
|        at Preconditions.checkIndex (Preconditions.java:248)
|        at Objects.checkIndex (Objects.java:372)
|        at ImmutableCollections$List0.get (ImmutableCollections.java:106)
|        at (#6:1)


JDK11-ea14

jshell> List.of().get(0)
|  Exception java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
|        at ImmutableCollections$ListN.get (ImmutableCollections.java:411)
|        at (#3:1)

System

arraycopy

The message become gentle for human.
JDK10

jshell> System.arraycopy(new int[0],0,new double[0],0,0)
|  java.lang.ArrayStoreException thrown


JDK11ea19

jshell> System.arraycopy(new int[0], 0, new double[0], 0, 0)
|  Exception java.lang.ArrayStoreException: arraycopy: type mismatch: can not copy int[] into double[]
setProperty(String, String)

So far changing java.home would make trouble but now it is not affect for the system, maybe...

Base64

Since ea20, the encoding become faster by using AVX512, but I can not confirm on Windows.

Boolean

parseBoolean

It become lettle faster to remove redundant null check, they said.
JDK10

    public static boolean parseBoolean(String s) {
        return ((s != null) && s.equalsIgnoreCase("true"));
    }


JDK11

    public static boolean parseBoolean(String s) {
        return "true".equalsIgnoreCase(s);
    }

I haven't confirmed it yet.

List

copyOf

copyOf that has introduced since Java10, it was wrong copying a subList. It could not be serialized. Now fixed.
Common initialize.

jshell> var list1 = List.of("a","b","c")
list1 ==> [a, b, c]

jshell> var list2=list1.subList(1,2)
list2 ==> [b]

jshell> var list3=List.copyOf(list2)
list3 ==> [b]


JDK11ea19

jshell> list2==list3
$13 ==> true

jshell> new ObjectOutputStream(OutputStream.nullOutputStream()).writeObject(list3)
|  Exception java.io.NotSerializableException: java.util.ImmutableCollections$SubList
|        at ObjectOutputStream.writeObject0 (ObjectOutputStream.java:1185)
|        at ObjectOutputStream.writeObject (ObjectOutputStream.java:349)
|        at (#14:1)


JDK11ea20

jshell> list2==list3
$26 ==> false

jshell> new ObjectOutputStream(OutputStream.nullOutputStream()).writeObject(list3)

jshell>

TimSort

There are some bug, but fixed, maybe.
https://blog.satotaichi.info/timsort-was-broken/