API Updates in Java SE 11 (18.9)
Java SE 11, which is also named as 18.9 (based on the new naming scheme which uses the year and month of release), is slated to be GA during the last week of September. The new approach for releasing new JDK version frequently is allowing the language creators to introduce new features, API updates more quickly to the developer community.
Often API updates go unnoticed and are buried under some major changes. So I thought of enumerating some of the API changes that would be in Java 11 which were not present in Java 10.
I am using the jdk-11-ea+16 build downloaded from here
Character.toString(int)
This method returns the string representation for the given Unicode code point as shown below:
jshell> Character.toString(100) $10 ==> "d" jshell> Character.toString(66) $7 ==> "B"
CharacterSequence.compare(java.lang.CharSequence, java.lang.CharSequence)
This compares two character sequence lexicographically and return negative, zero or positive if first character sequence is lexicographically less than or equal to or greater than the second respectively.
Lexicographically means dictionary order or alphabetical order.
jshell> CharSequence.compare("girl", "boy") $12 ==> 5 jshell> CharSequence.compare("girl", "girl") $13 ==> 0 jshell> CharSequence.compare("hello", "world") $14 ==> -15
New APIs in java.lang.String
repeat(int)
jshell> "**".repeat(5) $15 ==> "**********" jshell> "**".repeat(-7) | Exception java.lang.IllegalArgumentException: count is negative: -7 | at String.repeat (String.java:3147) | at (#16:1) jshell> "**".repeat(0) $17 ==> "" jshell> "**".repeat(1) $18 ==> "**"
isBlank()
jshell> String msg = "hello" msg ==> "hello" jshell> msg.isBlank() $22 ==> false jshell> String msg = "" msg ==> "" jshell> msg.isBlank() $24 ==> true jshell> String msg = " " msg ==> " " jshell> msg.isBlank() $26 ==> true
strip(), stripTrailing(), stripLeading()
jshell> " hello world ".strip() $29 ==> "hello world" jshell> "hello world ".strip() $30 ==> "hello world" jshell> "hello world ".stripTrailing() $31 ==> "hello world" jshell> " hello world ".stripLeading() $32 ==> "hello world " jshell> " ".strip() $33 ==> ""
lines()
jshell> String content = "this is a multiline content\nMostly obtained from some file\rwhich we will break into lines\r\nusing the new api" content ==> "this is a multiline content\nMostly obtained fro ... ines\r\nusing the new api" jshell> content.lines() $36 ==> java.util.stream.ReferencePipeline$Head@5ec0a365 jshell> content.lines().forEach(System.out::println) this is a multiline content Mostly obtained from some file which we will break into lines using the new api
java.nio.file.Path.of()
Prior to this release, there were no factory methods in java.nio.file.Path
, while there was one method in java.nio.file.Paths
. This release introduces a factory method in java.nio.file.Path
of which there are two variants:
1. Takes String location to the resource
2. Takes URI location to the resource
Both of them are shown below:
jshell> Path uriPath = Path.of(new URI("file:///C:/Program%20Files/Java/jdk-11/release")) uriPath ==> C:\Program Files\Java\jdk-11\release jshell> Files.readAllLines(uriPath).forEach(System.out::println) jshell> Path filePath = Path.of("..", "release") filePath ==> ..\release jshell> Files.readAllLines(filePath).forEach(System.out::println)
Pattern.asMatchPredicate()
This API returns a java.util.function.Predicate
which can be used to test if a given string matches the pattern compiled using the java.util.regex.Pattern
jshell> Pattern somePattern = Pattern.compile("\\w+@\\w+[.]com") somePattern ==> \w+@\w+[.]com jshell> Predicate<String> somePredicate = somePattern.asMatchPredicate() somePredicate ==> java.util.regex.Pattern$$Lambda$26/0x00000008000d0840@34c4973 jshell> somePredicate.test("sana@gmail.net") $55 ==> false jshell> somePredicate.test("sana@gmail.com") $56 ==> true jshell> somePredicate.test("sana#@gmail.com") $57 ==> false
The Java EE related APIs namely Corba, JAXB, JAX WS (Web Services) are being removed. The HTTP Client library which was in the incubator until Java 10 is being moved out of incubator into its own module java.net.http
. I will soon write some posts on the new HTTP Client.
Published on Java Code Geeks with permission by Mohamed Sanaulla, partner at our JCG program. See the original article here: API Updates in Java SE 11 (18.9) Opinions expressed by Java Code Geeks contributors are their own. |