Posts

Showing posts with the label java 9 and beyond

Convert time unit to duration in Java

java.util.concurrent.TimeUnit represents time durations in Java at a given unit of granularity and provides utility methods to convert across units. java.util.concurrent.TimeUnit was introduced back in the old Java days (1.5) but since then it has been extended several times already. In this blog post you will learn how to use java.util.concurrent.TimeUnit to convert a given time unit to a duration .

Switch as an expression in Java with Lambda-like syntax

As of Java 14, the switch expression has an additional Lambda-like ( case ... -> labels ) syntax and it can be used not only as a statement, but also as an expression that evaluates to a single value.

Record type in Java

Image
JDK 14, released in March 2020, introduced records (preview language feature) which provide a compact syntax for declaring classes whose main purpose is to hold data. In records , all low-level, repetitive and error-prone code is like constructors, accessor and utlity methods such as equals() , hashCode() , toString() are automatically derived based on the record’s state description.