Java

Java 13

This will cover some of the features that were introduced in Java 13 and were released officially on 13th of September 2019.

ZGC

It was introduced in Java 11. In this release it was enhanced to return memory to the system when the space is unused. This is a critical improvement because applications with a limited memory to run need to use every bit. Think of the cloud where every bit costs money. In a nutshell the improvement here is to clear up the ZPages from the ZCachePages using different policies. It’s always fun to invalidate caches.

https://image.slidesharecdn.com/advancedcacheinvalidation-140319145303-phpapp02/95/advanced-cache-invalidation-8-638.jpg

New Socket API implementation

Not much to say here. It was redesigned with fibers(Project Loom) in mind and use the NIO mechanism. The old implementation dates from JDK 1.0 and has lots of issues (mixed of Java and C code, native structures).

Text blocks

This is a preview feature so in order to use it you must activate the preview either from command line or you favourite IDE. It’s a stolen feature if you want. Golang for example had this quite for some time. But nevertheless it’s useful, just think of those long queries or jsons.

var json = """
  {
   "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
            }
 }
""";

New String methods

formatted(Object… args)

Added as support for text blocks.

var json = """
  {
   "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": %d,
        "vOffset": %d,
        "alignment": "center"
            }
 }
""".formatted(250,250);

stripIndent()

Removes whitespaces that are present at the start and the end of every line of the text block.

 var json = "{" +
            "\t\"image\": {         \n" +
            "\t\t\"src\":\"Images/Sun.png\",\n" +
            "\t\t\"name\":\"sun1\",\n" +
            "\t\t\"hOffset\": 250,\n" +
            "\t\t\"vOffset\": 250,\n" +
            "\t\t\"alignment\":\"center\"\n" +
            "\t\t}\n" +
            "}";
  System.out.println(json.replace(" ", "*"));
  System.out.println(json.stripIndent().replace(" ", "*"));

this will result in:

{	"image":*{*********
		"src":"Images/Sun.png",
		"name":"sun1",
		"hOffset":*250,
		"vOffset":*250,
		"alignment":"center"
		}
}
{	"image":*{
		"src":"Images/Sun.png",
		"name":"sun1",
		"hOffset":*250,
		"vOffset":*250,
		"alignment":"center"
		}
}

Did you spot the difference? Not that useful if you ask me.

translateEscapes()

This will determine which are the escape characters and will be applied when printing the string. I’m just mentioning this here as I find it even less useful than the above method.

Switch expressions (continued)

The below constructs have the same result.

public static int newSwitchExpression(String s) {
    return switch (s) {
      case "Foo" -> 1;
      case "Bar" -> 2;
      default -> {
        System.out.println("Neither Foo nor Bar, hmmm...");
        yield 0;
      }
    };
  }

public static int newSwitchExpression(String s) {
    return switch (s) {
      case "Foo":
        yield 1;
      case "Bar":
        yield 2;
      default:
        System.out.println("Neither Foo nor Bar, hmmm...");
        yield 0;
    };
  }

The improvements here include elimination of the break statements which were often causing unexpected behaviour, the possibility to use switch statement as a return value which is called now switch expression. In order to differentiate easily switch expressions from switch statements the following rule has been created: if it’s a switch expression use yield. Also switch expressions must be exhaustive which means there has to be a return value no matter what (a default block is required).

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.