loading...
Cover image for Java enters its teenage 🎉 🎂 ⒔

Java enters its teenage 🎉 🎂 ⒔

sendilkumarn profile image Sendil Kumar N ・4 min read

The Java 13 is released on 17th September.

With this release, Java is officially entering into its teenage.

JEP - 350 Dynamic CDS archives

The JVM loads the class from the disk. Once loaded, the generated bytecode will be verified.
The verified bytecode will be pulled into the internal data structures by JVM. These are stored
into an archive called "Class Data Sharing(CDS) Archives".

This initial loading will take some time to startup the application execution.
In order to speed up the start-up time of the application, we can use the CDS archive.
JVM instead of manually loading the class, verifying and setting them into its internal
data structures, It will take the information from the CDS archives.

Usually most of the Java applications will have thousands of classes to load during its startup.
This will add a few seconds in the application's bootup time.

Till Java 12, we have to manually provide a list of classes for JVM to archive. Using the -XX:DumpLoadedClassList=list-of-classes.

But from Java 13 with -XX:ArchiveClassesAtExit=app-cds.jsa. JVM will create the archive classes
at exit. The archive class app-cds.jsa.

Then when the application is started again JVM will use this archive to boot up faster.
We have to instruct the JVM to use the archive with -XXSharedArchiveFile=app-cds.jsa.

Note: Whenever there is a change in the classpath the archive is invalidated and the
JVM has to take a new snapshot of the archive.

Check out more about the Dynamic Class Data Sharing archives here
and more here on JEP-310 Class Data Sharing.

JEP - 354 Switch expressions (in preview mode)

JEP-325 introduced switch expressions as a preview feature in Java12.

We can either use case expression (or) function -> like below:

boolean isTrue = switch (valueToCheck) {
  case "True", "Truthy", "Correct", "Valid" -> true;
  case "False", "Falsey", "InCorrect", "Invalid" -> false;
  default -> throw new NotValidException("some valid message");
}

or case expression (or) function : :

boolean isTrue = switch (valueToCheck) {
  case "True", "Truthy", "Correct", "Valid":
    yield true;
  case "False", "Falsey", "InCorrect", "Invalid":
    yield false;
  default:
    throw new NotValidException("some valid message");
}

Check out more about the switch expressions here.

JEP - 355 Text blocks (in preview mode)

Multiline strings or text blocks are long-time expected the feature in Java. With a lot of SQL queries, XML, or HTML in Java applications,
it is both difficult and cumbersome to deal with multiline strings.

This feature enables us to write multiline string with indentation easy and less cumbersome.

In order to define a multiline string, we will have to wrap the string with """. Note that the string inside the """ is
indented exactly how it is represented. If you want to trim any spaces use stripIndent to remove any accidental spaces.


String html = """
              <html>
                  <body>
                      <p>Hello, world</p>
                  </body>
              </html>
              """;


String query = """
               SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB`
               WHERE `CITY` = 'INDIANAPOLIS'
               ORDER BY `EMP_ID`, `LAST_NAME`;
               """;

Check out more about the Text blocks here.

JEP - 351 ZGC: Uncommit Unused Memory

The Z Garbage Collector, also known as ZGC, is a scalable low latency garbage collector designed to meet the following goals:

  • Pause times do not exceed 10ms
  • Pause times do not increase with the heap or live-set size
  • Handle heaps ranging from a few hundred megabytes to multi terabytes in size

Running on the cloud, you should pay for what you use. In Java applications, GC will reserve the heap size that is
more than necessary for the application. It is important to release the unused memory in that low-resource
environments. The other GCs (GC1 and Shenandoah) will release the
unused memory after the mentioned time.

Similarly, we can configure ZGC also to release the unused memory after a certain period of time using the following
option -XX:ZUncommitDelay=<seconds>.

This will increase the resources that your application can use.

Check out more about this feature here.

Also, for Z Garbage Collector we can specify a maximum heap size using -XXSoftMaxHeapSize=<bytes>. This prevents
the heap size to be within the specified value. Remember it is a soft limit when there is an absolute necessity to
avoid OOM, the JVM can increase the heap size till -Xmx.

The Z Garbage Collector can now use 16TB of the heap size. (That is huge...).

JEP - 353 Reimplement the Legacy Socket API

The java.net.Socket and java.net.ServerSocket were a piece of old code that was written with some native code (Java
and C).

With NioSocketImpl which is a replacement for PlainSocketImpl. The native code is completely replaced with the New I/O
implementation (NIO).

It also replaces hard to debug synchronised methods with the concurrent locks.

curious why it is exciting to check here and here

This makes it easy to debug and also align with the fibres in the future.

Check out more about this feature here.

This release obviously makes Java more cloud-native friendly and modernise the language even more.

Discussion

pic
Editor guide
Collapse
mattmoranjava profile image
Matt Moran

Teenage in version numbers, sure - but Java's 24 years old now. :-)

Collapse
sendilkumarn profile image