31 March 2021

Java SE 16: some goodies

Record

Records are a simple form of classes intended for value objects. Example:

public record Expert (String name, String domain) {} 

Records are like classes for immutable data with default boilerplate code added

  • all attributes are private
  • Constructor with arguments for all attributes
  • getters
  •  equals and hashCode methods
  • toString()

This is similar to the conventions used in Lombok and Kotlin data classes.

Methods can be added to the class and default methods can be overridden.

Limitations

  • no inheritance: records cannot extend classes and are implicitly final
  • extra attributes must be static

instanceof instanciation

Classic code

if (car instanceof F1) {
    F1 redBull = (F1) car;
    redBull.race();
}

Java 16

if (car instanceof F1 redBull) {
  redBull.race();  

}

jpackage

jpackage is a command to generate an installer package for you project. It can

  •     generate a classical jar for you, but adds a  startup scrip including all necessary classpaths
  •     include the JRE (or just the modules you need) to have a n installer without dependencies
  •     produce .exe, .msi, .rpm ... pacakges

No comments:

Post a Comment