Good day to all. In November 2017, one of the most remarkable events of the year for domestic Java developers took place in St. Petersburg: the Joker conference. At the conference, many topics were voiced, such as GC, Concurrency, Spring Boot, JUnit 5 and others, presentations on which you can find in the public domain on the conference website. It makes no sense to list everything, since for each topic you can make a separate article with examples and excerpts. Therefore, we will focus on the main thing.
The main topic was innovations in Java 9: as many as two lectures were devoted to it, on modules and everything else. Oracle nine itself was originally planned to be released in the middle of summer 2016, but the release was postponed, first half a year, and then completely into the second half of 2017. And so, on September 21, 2017, the nine came out.
This article is presented exactly as a review of newly-created java, since the topic itself is a large one, requiring a whole series of articles, which will undoubtedly be upon receipt of requests from working people.
')
So, in order. As mentioned above, innovations in the nine can be divided into two blocks: general and modular. Adhering to the chronology of Joker, let's start with the first.
1. The appearance of literals in collections
In fact, literals in collections can be used from version 7, no one forbids doing the following if you have ProjectCoin installed:
List<Integer> list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9]; Set<Integer> set = { 2, 7, 31, 127, 8191, 131071, 524287 };
In version 9 you can use something like this without any preliminary preparations:
List<Integer> piDigits = List.of(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9); Set<Integer> primes = Set.of(2, 7, 31, 127, 8191, 131071, 524287);
or
import static java.util.Map.*; platonicSolids = ofEntries(entry(4, "tetrahedron"), entry(6, "cube"), ...);
Also, a few words were said about the generic Pair, or rather, about their absence. However, at 9, an improved crutch using Mapa as a pair appeared:
Map.entry(lowest, highest)
We are waiting for
Project Valhalla .
2. The introduction of the operator Elvis
In fact, this statement is implemented on Groovy at the syntactic level, but if you try to write something similar in Java
Person person = JohnGold ?: DefaultPerson;
the compiler predictably curses. Nine implemented similar logic, although not at the level of syntax. Below are two equivalent expressions on the nine and in the already familiar form.
person = Objects.requireNonNullElse(JohnGold, DefaultPerson)
3. Class Optional
In short, this class is for collecting not-null objects: instead of carrying out null checks, you can add objects to this container, which will automatically screen out non-existent objects. Appeared in Java 8 and it is logical that the new version has improvements for this class. The
or method is intuitive: the caller will be returned if it is not null, otherwise the argument will be returned.
maybePerson = maybePerson.or(() -> JohnGold);
The following method performs the specified action on the value, if it is present, otherwise some default value will be executed.
maybePerson.ifPresentOrElse(System.out::println,
If the value is present, it returns a stream with this value only, otherwise the stream will be empty.
Stream<T> stream()
Ability to use flatMap to remove zero results:
Stream<User> users = people.map(Person::name) .flatMap(Optional::stream);
4. Streams
A number of API methods have emerged from which streams can be obtained: Scanner.tokens, Matcher.results, ServiceLoader.stream, LocalDate.datesUntil,
StackWalker.walk, ClassLoader.resources, Process.children / descendants,
Catalog.catalogs, DriverManager.drivers. In the streams themselves, there are also new takeWhile, dropWhile methods, as well as new flatMapping, filtering collectors.
5. IO, Regrexp
Everything is also big. Now it is possible to read bytes from the incoming stream.
byte[] bytes = Files.newInputStream(path).readAllBytes();
Redirect bytes from the incoming stream to the outgoing stream: InputStream.transferTo (OutputStream).
Now it is possible to split an object received by the Scanner class into tokens as separate streams:
Stream<String> tokens = new Scanner(path).useDelimiter("\\s*,\\s*").tokens();
Matcher.stream and Scanner.findAll give a stream of results found:
Pattern pattern = Pattern.compile("[^,]"); Stream<String> matches = pattern.match(str).stream().map(MatchResult::group); matches = new Scanner(path).findAll(pattern).map(MatchResult::group);
Matcher.replaceFirst / replaceAll can now accept as input a function according to which the permutation will be made:
String result = Pattern.compile("\\pL{4,}") .matcher("Mary had a little lamb") .replaceAll(m -> m.group().toUpperCase());
6. Processing processes
Then a whole
ProcessHandle interface
appeared , allowing you to control native processes. With it, you can monitor whether the process is alive, information about it, a sheet of children and other buns. Why is it needed when there is an abstract class Process, you ask. In fact, ProcessHandle provides much more features than Process. The latter, in turn, also provides access to IOE streams, which affects performance.
7. A bit of syntax changes.
Interfaces can now have methods with access modifiers private, private static, and it seems that they will soon cease to be interfaces. The underscore
_ can no longer be a variable name - the nuts are tightened. And finally, try-with-resources support
void print (PrintWriter out, String[]lines){ try (out) {
8. Old age
There were several deprikatsiy, where without it. The Observable, Observer classes from the new version will be considered obsolete, as well as Object.finalize, Runtime.runFinalizersOnExit. Class.newInstance is also now referred to as Deprecated and argues that it throws checked constructor exceptions without declaring them. Under the same annotation, the whole Applet API, as well as a number of modules: java.activation, java.corba, java.transaction, java.xml.bind, java.xml.ws. Here were listed the main things that fall under the annotation Deprecated and in the presentation of Horstman, the full list can always be viewed at
oracle .
9. The most important news
System.getProperty("java.version")
Now this code will return the value "9", and not "1.9.0.".
Also in Java 9, there was a modularity that caused more noise than all of the above. And this is not surprising, since
statistics show that this modularity was the most expected feature among developers.

There were questions about what this is all about, how it would interact with Maven. Half of the report went for the
OSGi review, the second half for the
Jigsaw review and the explanation of why this is cool, exceeds the subject of the first half of the report in all respects and that Jigsaw is unjustly modestly accepted by society. We can only judge the success of this project from society, but for now we can only wait for a global transition to Java 9, which, as the speakers themselves said, the next 2-3 years will occur. In the meantime, you can look at the most anticipated changes in the next version of Java and listen to jokes about whether it will be called Java 10 or Java X.
