"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 has made changes to the release cycle (a new release every six months), the 11th version is being prepared for release.

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




    Java 10: a brief summary


    The 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 enter types twice in a row: first for declaring a variable, and then for the constructor that comes next.

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

    However, the decision to introduce var received mixed reviews from community members. Someone spoke out for innovation and said that deduplication improves code readability. But there were those who noted that now a number of types of variables (for example, connection) would not be obvious. Although IDEs can now display them on demand, problems will arise in other environments.

    For improved garbage collection, two changes were immediately included in the tenth release: JEP 304 and JEP 307 . JEP 304 improved isolation of code from various garbage collectors with the new GC interface, which allowed faster integration of third-party collectors. JEP 307 gave the opportunity to "collect garbage" in several threads.

    As for the new JIT compiler, it aims to improve JVM performance. The previous version of the JVM was written in C ++, however, as part of the Metropolis project, most of the JVM will be rewritten in Java. The experimental compiler is the first step towards this goal.


    / photo Betsy Weber CC

    Possible innovations in Java 11


    In early fall, developers plan to introduce Java 11. And we already know about a number of functions that may become part of the release today. The network even has a heated discussion of the proposed changes.

    Some developers are unhappy with how fast the language is changing. One resident of Hacker News said : “Java is turning into a completely new language. One of the reasons I used to use Java before is backward compatibility. If you 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 languages. Another HN user writes: “I have not used Java since the beginning of the 2000s and I am surprised that they have been introducing features that have existed in other languages ​​for so long.” Here is some of them.

    Changes to 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 possible to use when declaring formal parameters for implicitly typed lambda expressions.

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

    Adding Raw String Literals

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

    String html = "\n" +
                  "  \n" +
                  "    

    Hello World.

    \n" + " \n" + "\n";

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

    String html = `
                     
                       

    Hello World.

    `;

    The reverse apostrophe (`) is used to indicate the raw string. If you need to register an apostrophe inside the line itself, then in this case double (or triple, quadruple, etc.) inverse apostrophes are used to mark its borders:

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

    Switch expressions will appear

    Now the design of switch is 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 design will shrink:

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

    Moreover, if only break is encountered in the 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 forward towards the emergence of a pattern matching method .

    In addition to the above, Java may receive other changes. For example, support for value types or type variables in enum. These updates will not necessarily be part of Java 11, but you can probably expect them in the near future.



    PS What else are we writing about the First Corporate IaaS Blog:


    PPS Some materials from our blog on Habré:


    Also popular now: