Time for unnamed pattern in java
Last updated: Mar 2, 2024
Already as a preview in Java 21, unnamed pattern is finally released in Java 22. The new feature in java is just the continuation of the long term vision from the amber project. Maybe it’s a good time to put it together and look back from Java 14.
In Java 14, jdk team integrated a better version of instanceOf. No need to declare and cast anymore.
if(animal instance Cat cat) {
cat.maouh();
}
It’s time to move on to Java 15. Sealed classes or interfaces can be use to add constraints such as:
- Every subclasses must explicitly extend the sealed classes
- The sealed type must declared all this subclasses with the keyword permits (not needed if subclasses are in the same file
- Every subclasses must be final
In Java 16, record type allows us to create an immutable data class. Guess what, their purpose is too hold data. It’s the end of javabean and boilerplate code. For example, to create a tuple:
record ProductWithPrice(String product, BigDecimal price) {}
Then a first version of a pattern matching was released and our switch keyword allows us to use the enhanced instanceOf:
String value = switch(obj) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Object o -> String.format("obj %s", o.toString());
}
Also, sealed interfaces provide a way to update our code to something safer and more readable. Is it the end of the pattern visitor. It’s a good practice not to use default case to insure that all cases are managed.
sealed interface TaxRate permit EuTaxRate, UsTaxRate {}
record EuTaxRate extend TaxRate {}
record UsTaxRate extend TaxRate {}
BigDecimal tax = switch(obj) {
case EuTaxRate eu -> eu.tax();
case UsTaxRate us -> us.tax();
}
Disqus comments are disabled.