Java

Java 11 features

It was release on September 25, 2018. This version has LTS so it is recommended to upgrade to this one in production. Unfortunately this is the first version that we need to pay for(oracle version). The openjdk is still open-source so this will be the choice for most of the developers. Oracle version is free for non-production use. Check out the rates for prod. Let’s see what are the features that were introduced.

String Additions

System.out.println(" ".isBlank());
//true

String str = "Learn\nWrite\nRepeat"; 
System.out.println(str.lines().collect(Collectors.toList())); 
//Learn
//Write
//Repeat

String str1 = "Learn Write Repeat ".repeat(2);
// Learn Write Repeat Learn Write Repeat

String str2 = " Learn Write Repeat".stripLeading();
//Learn Write Repeat - it eliminate leading space

String str3 = "Learn Write Repeat ".stripTrailing();
//Learn Write Repeat - it eliminate trailing space

String str4 = " Learn Write Repeat ".strip();
//Learn Write Repeat - it eliminate both leading and trailing space

File Additions

//write string to file
Files.writeString(Path.of(file.txt), "Learn Write Repeat!");

//read from file
Files.readString(Path.of(file.txt));

isSameFile() is used to know whether two paths locate the same file or not. The difference between Path.equals is that isSameFile() tries to locate the file on the filesystem and use it for comparison, it must not necessary be on the same filesystem.

Files.isSameFile(Path.of("file.txt"), Path.of("file.txt"));
//true

var for Lambda Parameters

List<Integer> evenList = List.of(2,4,6,7,8,9)
      .stream()
      .filter((var i) -> i % 2 == 0) 
      .collect(Collectors.toUnmodifiableList());

this can be rewritten to

List<Integer> evenList = List.of(2,4,6,7,8,9)
      .stream()
      .filter(i -> i % 2 == 0)
      .collect(Collectors.toUnmodifiableList());

Optional Additions

We all know the problem with Optional.get(). Basically we need to do a Optional.isPresent() before. In the case we want to do something if the value is empty we do

if (!Optional.isPresent()){
   // do something when not present
}

//with 11 we can do
if (Optional.isEmpty()){
   // do something when not present
}

Epsilon GC

Since in java 10 it was introduced an interface for GC it’s normal that multiple implementations should appear. This was designed specially for short term jobs. Basically it does not performs GC at all which means it be used for performance testing amongst others. This is experimental though, so definitely should not be used in prod.

-XX:+UnlockExperimentalVMOptions
-XX:+UseEpsilonGC

ZGC

Another implementation of GC. It’s significant different from the other GCs. It uses two new techniques: coloured pointers and load barriers.

Coloured pointers refers to the usage of some bits from the 64bits address to store information. This is possible due to the fact that the pointer can address more memory than the system can have. The problem with this is that we need to dereference the pointer because we need to mask out the information bits. Multi mapping technique helps in this situation. This involves mapping different ranges of virtual memory to the same physical memory.

Load barriers is basically code that runs when a reference is loaded from the heap. If we have for example a local variable, instead of getting a reference from the heap it does not need to as it’s declared locally. ZGC is testing the loaded reference to see if certain bits are set or not and depending on this it knows if it needs to additional work.

-XX:+UnlockExperimentalVMOptions
-XX:+UseZGC

ZGC cycle starts with marking. Like others GC this consists of finding the live objects. It starts with the GC roots and goes down the graph. If if finds unreachable objects they will be not marked. Then a mask will be applied to check if the object was already marked or not. If not it will be added in the marking queue. Then if follows the next major part of the cycle which involves relocation of the live objects to free up space. The heap is divided into pages. Then it selects a set of pages and a memory where to relocate them. More detailed information can be found here.

Flight Recorder

This is a profiling tool. It was open sourced with this java release. More info here.

1 thought on “Java 11 features”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.