Java enters its teenage 🎉 🎂
Tags: java, programming, jvm, features
Published: 2019-09-22 19:47:30

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](http://openjdk.java.net/jeps/350)
and more [here](https://openjdk.java.net/jeps/310) on JEP-310 Class Data Sharing.

# JEP - 354 Switch expressions (in preview mode)

[JEP-325](https://openjdk.java.net/jeps/325) introduced switch expressions as a preview feature in Java12.

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

```java
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 :` :

```java
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](http://openjdk.java.net/jeps/350).

# 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.

```java

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](http://openjdk.java.net/jeps/355).

# JEP - 351 ZGC: Uncommit Unused Memory

The [Z Garbage Collector](https://wiki.openjdk.java.net/display/zgc/Main), 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](https://wiki.openjdk.java.net/display/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](http://openjdk.java.net/jeps/351).

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](https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/net/Socket.java) and [java.net.ServerSocket](https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/net/ServerSocket.java) were a piece of old code that was written with some native code (Java
and C).

With `NioSocketImpl` which is a replacement for [PlainSocketImpl](https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/net/AbstractPlainSocketImpl.java). 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](https://stackoverflow.com/questions/4201713/synchronization-vs-lock) and [here](https://winterbe.com/posts/2015/04/30/java8-concurrency-tutorial-synchronized-locks-examples/)

This makes it easy to debug and also align with the [`fibres`](https://wiki.openjdk.java.net/display/loom/Main) in the future.

Check out more about this feature [here](http://openjdk.java.net/jeps/353).

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