Java 11 API Change Proposals
Want to know what's next for Java 11? Check out this post on the proposed Java 11 API changes to learn more.
Join the DZone community and get the full member experience.
Join For FreeJDK11 features had been frozen since it entered the ramp-down phase last month. The big changes are listed as JEPs. In addition, there are many changes outside of the JEPs in JDK 11, but no summary exists. Therefore, I've listed 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 number of times:
jshell> "test".repeat(3)
$7 ==> "testtesttest"
isBlank()
This method determines whether the string contains spaces. Full-width spaces are also treated as spaces.
jshell> var halfSpace = "\u0020"
halfSpace ==> " "
jshell> halfSpace.isBlank()
$11 ==> true
jshell> var fullSpace = "\u3000"
fullSpace ==> " "
jshell> fullSpace.isBlank()
$13 ==> true
strip()/stripLeading()/stripTrailing()
While almost the same as trim()
/trimLeft()
/trimRight()
, this takes full-width spaces as a space.
jshell> var aaa = fullSpace + "aaa" + fullSpace
aaa ==> " aaa "
jshell> aaa.strip()
$14 ==> "aaa"
jshell> aaa.trim()
$15 ==> " aaa "
CharSequence
compare(CharSequence, CharSequence)
Sort by dictionary order.
It is used by compareTo()
in CharSequence
/StringBuffer
/StringBuilder
. Therefore, the three classes implement Comparable
now.
Character
toString(int)
JDK 11 makes this process more 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()
. Now, we can use of()
in the same manner as other classes.
Files
writeString(Path, CharSequence)
We can save a String with one method.
jshell> Files.writeString(Path.of("test.txt"), "Hello!!!")
$3 ==> test.txt
readString(Path)
We can read a String with one method.
jshell> Files.readString(Path.of("test.txt"))
$4 ==> "Hello!!!"
Reader
nullReader()
We can get a Reader
that does nothing.
Writer
nullWriter()
We can get a Writer
that does nothing.
InputStream
nullInputStream()
We can get an InputStream
that does nothing.
OutputStream
nullOutputStream()
We can get an OutputStream
that does nothing.
Predicate
not(Predicate)
We could choose to not use a method reference where it needs to invert a condition. Instead, 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())])
to create a 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 before now. Now, we haveisEmpty()
.
jshell> Optional.ofNullable(null).isEmpty()
$5 ==> true
TimeUnit
convert(Duration)
This has been added for java.util.concurrent.TimeUnit
.
Pattern
asMatchPredicate()
Up until now, there has only been asPredicate,
but, now, we have asMatchPredicate
.
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
ListSelectionModel
getSelectedIndices()
/ getSelectedCount()
have been added.
Thread
destroy()/stop(Throwable)
Removed destroy()
. stop()
remains.
Policy
javax.security.auth.Policy
has been removed.
ArrayIndexOutOfBoundsException
The exception message has changed:
JDK10.0.1
jshell> new int[]{}[0]
| java.lang.ArrayIndexOutOfBoundsException thrown: 0
| at (#8:1)
JDK11ea14
jshell> new int[]{}[0]
| Exception java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
| at (#4:1)
IndexOutOfBoundsException
In this change, the hyphens have been removed in this 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)
JDK11ea14
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
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)
Before, changing java.home
would cause some trouble. But, now, the issues have been resolved.
Supporting Japanese New Era
The Japanese Imperial Era is scheduled to change to the new era on May 1st, 2019.
It is introduced as a NewEra
placeholder.
It will be updated in JDK 12.0.1 after the Japanese government announces it.
JDK10.0.1
jshell> JapaneseDate.of(2019, 5, 1)
$15 ==> Japanese Heisei 31-05-01
JDK11 ea18
jshell> JapaneseDate.of(2019, 5, 1)
$3 ==> Japanese NewEra 1-05-01
We have not been able to get May 1st Heisei 31 as our JapaneseDate.
JDK10.0.1
jshell> JapaneseDate.of(JapaneseEra.HEISEI, 31, 5, 1)
$14 ==> Japanese Heisei 31-05-01
JDK11 ea18
jshell> JapaneseDate.of(JapaneseEra.HEISEI, 31, 5, 1)
| Exception java.time.DateTimeException: year, month, and day not valid for Era
| at JapaneseDate.of (JapaneseDate.java:231)
| at (#2:1)
Base64
Since ea20, the encoding has become faster by using AVX512, but I cannot confirm it on Windows.
Boolean
parseBoolean
It has become a little faster to remove the redundant null check, they have claimed.
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 the performance difference yet.
TimSort
TimSort is the main algorithm that is used in Array.sort()
and Collection.sort()
.
It had a bug where it threw an ArrayIndexOutOfBoundsException
for some sequences, but that seems to be fixed. I haven't confirmed it yet.
Published at DZone with permission of Naoki Kishida. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments