5. 12/5/19
Other JDKs
➢
OracleJDK: Free and supported to 6 months
➢
LTS versions have extended ($$$) support by Oracle
➢
Identical to OpenJDK when free
➢
OpenJDK: Free forever, not supported after 6 months
➢
Built by https://adoptopenjdk.net/
➢
Other JVMs (Azul, IBM, RedHat, etc.)
5
7. 12/5/19
Java 9 Features with Examples
2. Collection Factory Methods
- Defines several factory methods (.of(…)) on the List, Set, and Map
interfaces for conveniently creating instances of unmodifiable collections
and maps with small numbers of elements.
- Map.of() is not a varargs method. There are only overloaded Map.of() for up to 10 entries. For a
case where we have more than 10 key-value pairs, there is a different method:
static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)
Reference: JEP 269
7
8. 12/5/19
Java 9 Features with Examples
3. Try-With-Resources Improvements
Before Java 9
FileInputStream fis = new FileInputStream("movie.mp4");
try (FileInputStream fis2 = fis)
{
//…..
}
catch (IOException e)
{
// ...
}
Reference: JEP 213
8
Java 9+
FileInputStream fis = new FileInputStream("movie.mp4");
try (fis)
{
//…..
}
catch (IOException e)
{
// ...
}
9. 12/5/19
Java 9 Features with Examples
4. Process API Improvements
Two new interfcase in Process API:
java.lang.ProcessHandle
java.lang.ProcessHandle.Info
The ProcessHandle provides:
- The process ID: process.getPid()
- Arguments
- Command
- Start time
- Accumulated CPU time
- User
- Parent process
With the ProcessHandle.onExit method, the asynchronous mechanisms of the CompletableFuture
class can perform an action when the process exits.
Reference: JEP 102
9
10. 12/5/19
Java 9 Features with Examples
5. New Http client API
> The API consists of 3 core classes:
HttpRequest – represents the request to be sent via the HttpClient
HttpClient – behaves as a container for configuration information common to multiple requests
HttpResponse – represents the result of an HttpRequest call
> Send Requests – Sync vs. Async
send(…) – synchronously (blocks until the response comes)
sendAsync(…) – asynchronously (doesn’t wait for response, non-blocking)
Reference: JEP 110
10
11. 12/5/19
Java 9 Features with Examples
6. Optional Class Improvements
●
stream() method:
●
public Stream<T> stream()
●
ifPresentOrElse() method:
●
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)
●
or() method:
●
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)
Reference: JEP-213
11
12. 12/5/19
Java 9 Features with Examples
7. Enhanced @Deprecated annotation
@Deprecated annotation provides better information about the status and intended future of APIs:
➢
forRemoval():
●
This returns a boolean to indicate whether this API element is intended for removal in some future
release.
●
E.g. @Deprecated(forRemoval=true) indicates the API will be removed in the next release of the
Java SE platform.
➢
Since():
●
This returns the release or version number, as a string, when this API element was first marked as
deprecated
@Deprecated(since="9", forRemoval=true)
Reference: JEP 277
12
13. 12/5/19
Java 9 Features with Examples
8. G1 (Default Garbage Collector)
Reference: JEP 248
13
14. 12/5/19
Java 9 Features with Examples
9. StackWalker API
➢
New API for easier manipulation of stack traces
➢
Provided by the java.lang.StackWalker class
Reference: JEP 259
14
15. 12/5/19
Java 9 Features with Examples
10. Compact Strings Improvement
➢
Prior to Java 9, string data was stored as an array of chars. This required 16 bits for each char.
public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
//The value is used for character storage.
private final char value[ ];
}
➢
Starting with Java 9, strings are now internally represented using a byte array along with a flag field for
encoding references.
public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final byte[] value;
private final byte coder;
}
Reference: JEP 280
15
16. 12/5/19
Java 10 Features with Examples
1. JEP 286: Local Variable Type Inference
var list = new ArrayList<String>(); // infers ArrayList<String>
var stream = list.stream(); // infers Stream<String>
2. JEP 307: Parallel Full GC for G1
3. Collection Changes
java.util.List: E> List<E> copyOf(Collection<? extends E> coll)
java.util.Set: <E> Set<E> copyOf(Collection<? extends E> coll)
java.util.Map: <K,V> Map<K,V> copyOf(Map<? extends K, ? extends V> coll)
16
17. 12/5/19
Java 11 Features with Examples
1. JEP 323: Local-Variable Syntax for Lambda Parameters
list.stream()
.map((var s) -> s.toLowerCase())
.collect(Collectors.toList());
list.stream()
.map((@Notnull var s) -> s.toLowerCase())
.collect(Collectors.toList());
2. JEP 330: Launch Single-File Source-Code Programs
java HelloWorld.java
3. JEP 321: HTTP Client (Standard)
➢
HTTP/2 support
➢
Incubating module from JDK 9 standardised
4. JEP 320: Remove The Java EE and CORBA Modules
17
18. 12/5/19
Java 11 Features with Examples
5. New APIs
●
java.io.ByteArrayOutputStream
- void writeBytes(byte[]): Write all the bytes of the parameter to the output stream
●
java.io.FileReader
- Two new constructors that allow a Charset to be specified.
●
java.io.FileWriter
- Four new constructors that allow a Charset to be specified.
●
java.io.InputStream
- io.InputStream nullInputStream()
●
java.io.OutputStream
- io.OutputStream nullOutputStream()
●
java.io.Reader
- io.Reader nullReader()
●
java.io.Writer
- io.Writer nullWriter()
●
java.lang.String
- boolean isBlank(), Stream lines(), String repeat(int), String strip(), String stripLeading(), String
stripTrailing()
18
19. 12/5/19
Java 11 Features with Examples
●
java.lang.Thread:
- destroy() and stop(Throwable) methods have been removed
●
java.nio.file.Files
- String readString(Path): Reads all content from a file into a string, decoding from bytes to characters
using the UTF-8 charset.
- String readString(Path,Charset): As above, except decoding from bytes to characters using the
specified Charset.
- Path writeString(Path, CharSequence, java.nio.file. OpenOption[]:Write a CharSequence to a file.
Characters are encoded into bytes using the UTF-8 charset.
- PathwriteString(Path,CharSequence,java.nio.file.Charset, OpenOption[]: As above, except Characters
are encoded into bytes using the specified Charset.
●
java.nio.file.Path
- Path of(String, String[]): Returns a Path by converting a path string, or a sequence of strings that when
joined form a path string.
- Path of(net.URI): Returns a Path by converting a URI.
●
java.util.Collection
- Object[] toArray(java.util.function.IntFunction): Returns an array containing all of the elements in this
collection, using the provided generator function to allocate the returned array.
●
java.util.function.Predicate
- Predicate not(Predicate)
19
20. 12/5/19
Java 11 Features with Examples
20
6. Removed Features
●
Removal of sun.misc.Unsafe.defineClass: Users should use the public replacement,
java.lang.invoke.MethodHandles.Lookup.defineClass
●
Removal of JavaFX from the Oracle JDK
●
…………