📜 ⬆️ ⬇️

“Life after Java 10”: what changes Java 11 will bring

Just recently, at the end of March, Java 10 was released . But due to the fact that Oracle made changes to the release cycle (a new release every six months), the 11th version is being prepared.

The launch is scheduled for September 2018. We offer a look at some of the upcoming updates that are known today.


/ photo by Markus Spiske PD
')

Java 10 brief


Innovations of the tenth version are: local type inference with var , improvements in garbage collection processes and the ability to use Graal as the main JIT compiler.

Local type inference with var was requested by many developers. Now you can not type in two times in a row: first to declare a variable, and then for the constructor that follows.

var list = new ArrayList<String>(); // infers ArrayList<String> var stream = list.stream(); // infers Stream<String> 

However, the decision to introduce var received mixed feedback from community members. Someone was in favor of innovation and said that deduplication increases the readability of the code. But there were those who noted that now a number of variable types (for example, connection) will not be obvious. And while IDEs can now display them on demand, there will be problems in other environments.

For improved garbage collection, two changes were immediately included in the tenth release: JEP 304 and JEP 307 . JEP 304 improved code isolation from various garbage collectors due to the new GC interface, which allowed integrating third-party collectors more quickly. JEP 307, on the other hand, made it possible to “collect garbage” in several streams.

As for the new JIT compiler, it is aimed at improving the performance of the JVM. The old version of JVM was written in C ++, but in the Metropolis project, most of the JVM will be rewritten in Java. The experimental compiler is the first step towards this goal.


/ photo by Betsy Weber CC

Possible innovations in Java 11


At the beginning of autumn, developers plan to submit Java 11. And a number of functions that can be part of the release are already known today. The network even has a heated discussion of the proposed changes.

Some developers are unhappy with how quickly the language is changing. One resident at Hacker News said : “Java is becoming a completely new language. One of the reasons why I used Java before is backward compatibility. If I have to learn a new programming language every 6 months, I will learn something else. ”

But there are those who note that Java is finally acquiring functions that it lacked, and which have long been implemented in other PL. Another HN user writes: “I have not used Java since the beginning of the zero and I am surprised that they have introduced features that have been existing in other languages ​​for so long”. Here are some of them.

Changes in local type inference

Java 10 has already provided the ability to use var to indicate the type of local variables, shifting this task to the compiler. However, Java 11 goes further and makes var so that you can use var when declaring formal parameters to implicitly typed lambda expressions.

For a detailed guide on when and how to use var, you can refer to this article on OpenJDK.

Adding raw string literals

This is another addition that is being worked on. In the raw string, each character is read "as is", including the break characters. For example, such a string could be HTML markup or an SQL query:

 String html = "<html>\n" + " <body>\n" + " <p>Hello World.</p>\n" + " </body>\n" + "</html>\n"; 

Using a string literal, this code can be written as follows:

 String html = `<html> <body> <p>Hello World.</p> </body> </html> `; 

To denote a raw line, use a reverse apostrophe (`). If you need to register an apostrophe within the line itself, then in this case, double (or triple, quad, etc.) reverse apostrophes are used to mark its borders:

 String str = ```This is a raw `string` with ``backticks`` inside```; 

Switch statements will appear.

Now the switch is executed as follows:

 int val; switch (str) { case "foo": val = 1; break; case "bar": val = 2; break; case "baz": val = 3; break; default: val = -1; } 

With the advent of switch-expressions , the construction will be reduced:

 int val = switch (str) { case "foo": break 1; case "bar": break 2; case "baz": break 3; default: break -1; } 

Moreover, if only break is found in a case, it is possible to use a simplified notation:

 int val = switch (str) { case "foo" -> 1; case "bar" -> 2; case "baz" -> 3; default -> -1; } 

Adding switch expressions is a step towards the emergence of the pattern matching method.

In addition to those noted above, Java may receive other changes. For example, support for types of values ​​or type variables in enum. These updates are not necessarily included in Java 11, but you can probably expect them in the near future.



PS What else do we write in the First blog about corporate IaaS:


PPS Some materials from our blog on Habré:

Source: https://habr.com/ru/post/358810/


All Articles