switch
keyword
The big picture: Java 9 key enhancement is the introduction of modules ( module
keyword) in order to rationalize the stuff present in the Java Virtual Machine (JVM) at execution time. Java 9-based design of an app. then arises from the “module” principle in the sense that developers have to isolate their code in execution-independent components.Rule(s)
- Much pre-programming code in the JVM may be excluded at run-time provided that any Java app. on the top of the executing JVM does not use this code; the footprint of the JVM becomes lower for the Operating System (OS). Typically, Java grand functionalities, for instance CORBA (Common Object Request Broker Architecture) embodied in the API by the
org.omg.CORBA
package, benefit from being evicted if unused by any given Java app.- Beyond, from Java 9, a great effort is on optimization at large: representation of
String
instances in memory, redesign of metadata in.class
files, etc.- Improvements related to Java 10, 11, 12, 13, 14, 15… clearly are rationalizations through “componentization”: performance, security, etc. Java 11 for example comes with the removal of superfluous functionalities: CORBA is one of the “fired” grand functionality.
Modules
Rule(s)
- Modules introduce a runtime dependency between consistent code blocks beyond the famous
classpath
Operating System variable that mixes in a confusing way, compilation and runtime dependencies (an interesting tutorial -in French- is here…).- The Java Virtual Machine (JVM) from ver. 9 is itself decomposed into modules to avoid massive code functionality, which is not required at all by any user-defined application. As shown, the Java 8 JVM is massive.
Further detail on Java 9 modules here…Immutable collections
Rule(s)
- The
of
static method ofjava.util.List
,java.util.Set
andjava.util.Map
allows the creation of immutable collections.Example From_Java_9.Java.zip
Mammutidae m0 = new Mammutidae("Macron"); Mammutidae m1 = new Mammutidae("Poutine"); Mammutidae m2 = new Mammutidae("Trump"); // Immutable collections (e.g., a set): try { java.util.Set.of(m1, m2).add(m0); // Poutine and Trump don't want Macron! } catch (UnsupportedOperationException uoe) { System.err.println("In essence, immutable collections do not support changes like additions: " + uoe.toString()); }
Example From_Java_9.Java.zip
java.util.Set<Integer> primes = new java.util.HashSet<>() { // <- Creation of an anonymous inner class, which extends 'java.util.HashSet' { // <- Block initializer add(2); add(3); } }; try { // Java 10 'copyOf': java.util.Set.copyOf(primes).add(4); } catch (UnsupportedOperationException uoe) { System.err.println("In essence, immutable collections do not support changes like additions: " + uoe.toString()); }
Streams improved…
Rule(s)
- Streams benefit from slight API enhancements in Java 9 (here…).
Example From_Java_9.Java.zip
java.util.List<Integer> positives = java.util.Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); java.util.stream.Stream<Integer> stream = positives.stream().filter(p -> p > 1 && java.util.stream.IntStream.rangeClosed(2, (int) Math.sqrt(p)).noneMatch(i -> (p % i == 0))); System.out.println(stream.takeWhile(p -> !p.equals(7)).collect(java.util.stream.Collectors.toList())); // '[2, 3, 5]' is displayed... java.util.stream.Stream<Integer> stream_ = positives.stream().filter(p -> p > 1 && java.util.stream.IntStream.rangeClosed(2, (int) Math.sqrt(p)).noneMatch(i -> (p % i == 0))); System.out.println(stream_.dropWhile(p -> !p.equals(7)).collect(java.util.stream.Collectors.toList())); // '[7, 11]' is displayed...
Rule(s)
- Streams in Java 8 cannot have
null
values. TheofNullable
static method in Java 9 improves the way of dealing withnull
.Example From_Java_9.Java.zip
java.util.List<Object> objects = java.util.Arrays.asList(new Object(), null, new Object()); java.util.stream.Stream<Object> stream__ = objects.stream().flatMap(o -> java.util.stream.Stream.ofNullable(o)); System.out.println(stream__.count()); // '2' is displayed...
As strongly typed programming language, Java imposes the typing of objects. Usages are then checked by the compiler in conformance with “announced” types. Contrary to JavaScript or Python, anomalies as potential execution errors are eliminated at compilation time (execution time for JavaScript or Python). Nonetheless, in many cases, “type announcement” creates no added value code. In this scope, type inference is the ability of the compiler to derive the type of an object. From Java 10, the var
keyword forces the compiler to establish the type an object on its own.
var
keyword (Java 10)Example (“old” type inference based on diamond-based instantiation)
java.util.Map<Integer, Double> polynomial = new java.util.HashMap<>(); polynomial.put(1, -12.D); polynomial.put(39, 8.D);
Example
var polynomial = new java.util.HashMap<Double, Double>(); polynomial.put(1, -12.D); polynomial.put(39, 8.D);
var
keyword in conjunction with lambda expression (Java 11)Rule(s)
- From Java 11, the
var
keyword is allowed in lambda expression syntax.Example From_Java_9.Java.zip
var positives = java.util.Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); var stream = positives.stream().filter((var p) -> p > 1 && java.util.stream.IntStream.rangeClosed(2, (int) Math.sqrt(p)).noneMatch((var i) -> (p % i == 0))); var primes = stream.collect(java.util.stream.Collectors.toList()); primes.forEach((var p) -> System.out.print(" " + p)); // '2 3 5 7 11' is displayed...
Rule(s)
- The
var
keyword may seem useless since the type of lambda expression parameters is “naturally” inferred. However, special cases show the relative interest ofvar
.Example (annotation imposes
Integer
andint
as types of lambda expression parameters) From_Java_9.Java.zip@java.lang.annotation.Target(value = java.lang.annotation.ElementType.PARAMETER) @interface Must_be_positive { } … var stream = positives.stream().filter((@Must_be_positive Integer p) -> p > 1 && java.util.stream.IntStream.rangeClosed(2, (int) Math.sqrt(p)).noneMatch((@Must_be_positive int i) -> (p % i == 0))); … primes.forEach((@Must_be_positive Integer p) -> System.out.print(" " + p)); // '2 3 5 7 11' is displayed...
Example (using
var
ejectsInteger
andint
) From_Java_9.Java.zip… var stream = positives.stream().filter((@Must_be_positive var p) -> p > 1 && java.util.stream.IntStream.rangeClosed(2, (int) Math.sqrt(p)).noneMatch((@Must_be_positive var i) -> (p % i == 0))); … primes.forEach((@Must_be_positive var p) -> System.out.print(" " + p)); // '2 3 5 7 11' is displayed...
Java 12 comes with much flexibility with the use of the historical switch
keyword.Rule(s)
- The
--enable-preview
(here…) is both a compilation and execution option to anticipate newer JVM features (if implemented as preview) without installing the very last JVM. This results from the accelerated release cadence of JVMs.Example From_Java_9.Java.zip
![]()
enum Direction { East, South, West, North } … var my_direction = Direction.East; var next_direction = switch (my_direction) { // '--enable-preview' case East -> /* 'break' disappears! */ Direction.South; case South, North -> /* 'break' disappears and multiple choices! */ Direction.East; default -> my_direction; };