JavaRush /Java Blog /Random EN /Java 12 is here: what's new?

Java 12 is here: what's new?

Published in the Random EN group
So, Java appeared at number 12. And even though this is not an LTS release (that is, not Long term support, not a release that will be supported for a long time. Version 11 is considered the first LTS release with an eight-year support period ), interesting things appeared in it updates. Let's look at the most important of them. Recall that in Java, change proposals are abbreviated as jeps (from JDK Enhancement Proposal). Java 12 is here: what's new?  - 1

The most interesting Jep's JDK 12

189: Shenandoah: A Low-Pause-Time Garbage Collector (Experimental) The Garbage Collector, Shenandoah, uses a new algorithm that reduces runtime by cleaning up simultaneously with running Java threads. In this case, the pause time in Shenandoah will be the same regardless of the size of the heap. For some reason, Oracle did not include Sheandoah in its “official” release builds, despite the fact that the developers consider this feature to be one of the most important in the new release. So if you plan to try out the advanced Garbage Collector, you need to use third-party builds, such as Azul . 230: Microbenchmark Suite Microbenchmark is a microbenchmark that measures the performance of some small element. In Java, they are written and run using the JMH framework. Since version 12, JMH has been added to the JDK, and there are already tests that are written in it, which makes life easier for developers. 325: Switch Expressions (Preview) A new form of writing an expression with a switch statement. The feature page provides example code using traditional switch, where constant use of break makes it unnecessarily verbose, leading to frequent random errors.
switch (day) {
    case MONDAY:
    case FRIDAY:
    case SUNDAY:
        System.out.println(6);
        break;
    case TUESDAY:
        System.out.println(7);
        break;
    case THURSDAY:
    case SATURDAY:
        System.out.println(8);
        break;
    case WEDNESDAY:
        System.out.println(9);
        break;
}
The authors of the update propose to introduce a new form of the switch label “case L ->” to clearly indicate that only the code to the right of the label will be executed if this label is suitable. Thus, the code shown above can be significantly shortened using the new syntax:
switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
    case TUESDAY                -> System.out.println(7);
    case THURSDAY, SATURDAY     -> System.out.println(8);
    case WEDNESDAY              -> System.out.println(9);
}
334: JVM Constants API The constant pool contains so-called loadable constants. These are values, for example of type String or any primitive type, as well as run-time artifacts, such as classes and methods. When working with class files, programmers would benefit from convenient tools for manipulating loadable constants. The authors of this jep began to solve this problem by introducing new value-based types of symbolic links, each of which will describe a specific type of constants. Innovation code . 340: One AArch64 Port, Not Two This complex name hides a solution to a problem that arose in Java 9, when 64-bit ARM ports of Oracle and aarch64 from Red Hat appeared in the kit at the same time. Moreover, already at the time of the release of JDK 11, even Oracle itself did not support the 64-bit version of the Oracle ports. So now the 64-bit Oracle ports have been removed, leaving 32-bit ARM32, and 64-bit, more productive, aarch64. 341: Default CDS Archives A very nice update as it really speeds up application launch. It is no secret that when a Java application starts, a huge number of classes begin to load, and this is a rather lengthy process. CDS stands for Class Data Sharing, and this function allows you to pack all these classes launched at startup into a special class data sharing archive, using the default list of these same classes. As a result, the speed of application launch increases significantly. 344: Abortable Mixed Collections for G1 G1 is a garbage collector that became the main garbage collector in JDK 9, replacing Parallel GC. In Java 10, G1 learned to collect garbage in multiple threads. However, not everyone was happy with his work; one of the main problems was long pauses. Now they can be canceled. G1, based on an analysis of the program’s behavior, determines the amount of work, and then collects “live” objects into a Collection set until it collects everything, without stopping. Often G1 missed the calculation of the amount of work and worked too long. Once jep-344 is implemented, G1 can switch to incremental garbage collection and if the next step takes longer to complete than is reasonable, that step can be aborted. 346: Promptly Return Unused Committed Memory from G1 And a few more updates in the camp of the aforementioned G1 assembler. Sometimes it happened that there was a bunch of Java heap memory, but no one used it, this memory. In Java 12, you can now “return” inactive memory to the operating system. Previously, it was very rare to get G1 to give up memory, but now everything has been simplified. With the new feature, the application is defined as inactive if the interval since the last build has expired and there is no concurrent cycle. Another situation is if or if the getloadavg() function, which has been monitored for some time, shows a load below the set acceptable threshold. If one of two events occurs, partial garbage collection begins. What Java 12 doesn't have: Raw String Literals Many developers were interested in this feature, but it never appeared in this release. Its authors decided that it still needed work. So, we dare to hope that a function for convenient work with string literals will appear in one of the next versions of Java. Support for raw string literals allows you to work with string information regardless of language. This eliminates the need for shielding. Particularly useful in regular expressions, where backslashes are widely used, and in Windows-path, where backslashes are used as delimiters. That is, to avoid such constructions: C:\\My\\Test\\Pack\\
Every programmer often encounters one or another difficulty in their work. Sometimes they are associated with the inexperience of the developer, but sometimes with the imperfection of the tool itself. Java is a great language, but it's far from perfect. Perhaps you have already encountered something in Java and know what you would like to change in your “tool” (language)? Share in the comments!

conclusions

There was no revolution in Java 12. However, no one planned it. However, a number of innovations are aimed at optimizing the operation of applications and speeding up work, which is good news. By the way, IDEA already supports JDK 12, so you can try out its capabilities. Of course, few updates directly affect beginners, although even those who have just started learning JavaRush can play with the new switch (after setting the --enable-preview flag).
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION