Binary literal: short aShort = (short)0b1010000101000101;
Underscore to enhance readability: long maxLong = 0x7fff_ffff_ffff_ffffL;
Try statement that declares one or more resources. A resource is as an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.
static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}
Map<String, List<String>> myMap = new HashMap<>();
With Java9, can provide private interface method (helper methods)
New methods added to the Stream interface: dropWhile, takeWhile, ofNullable.
IntStream.iterate(1, i -> i < 100, i -> i + 1).forEach(System.out::println); // new iterate method overload allow specify when to stop
Stream<Integer> s = Optional.of(1).stream(); // turn an Optional into a stream
Descriptor: module-info.java, which can "exports" packages therefore encapsulate other while public classes for unintended usage. Also express dependency through "requires" statement. This allow enforcement of encapsulation and dependency check.
With module descriptor, can use jlink tool to create a minimal runtime image, no need the entire runtime environment
Interactive Read-Eval-Print-Loop.
Set<Integer> ints = Set.of(1, 2, 3);
List<String> strings = List.of("first", "second");
The returned is IMMUTABLE
List.copyOf(list)... toUnmodifiableList...
Package backward compatible classes in traditional jar path while create new path for like java 9.
var variable_name = ....; // type inferred at compile time, must with initialization, cannot compound (two at a time)