Features Java 16 Brings to Developers
Tags: java, programming, java16, java8
Published: 2021-03-28 20:33:02


[Živković Miloš](https://zivce.medium.com)

## Learn about pattern matching, sealed classes, and more new Java features.

Java needs no introduction. It’s a programming language with many years under its belt. Recent updates brought several new features to the language: Sealed classes, pattern matching, and additional safety measures.

# Sealed classes

[JEP-397](https://openjdk.java.net/jeps/397)

The motivation behind `sealed` is to restrict concrete classes. Restricting concrete classes, will control implementation, and provide support for pattern matching.

// keyword here is sealed which enables permits  
// permits lists classes that can extend sealed class public abstract sealed class Shape  
permits Circle, Rectangle, Square { ... }

With basic interfacing, you don’t get reflectiveness. You don’t know about a finite set of concrete implementations.

With sealed classes, you get this reflective behavior. You know about the _kind_ of types, you can have in your domain.

Enums tell about a finite set of values. Sealed classes tell about a finite amount of kind of values.

sealed interface Celestial  
permits Planet, Star, Comet { ... }  
  
final class Planet implements Celestial { ... }  
final class Star   implements Celestial { ... }  
final class Comet  implements Celestial { ... }

> This hierarchy does not, however, reflect the important domain knowledge that there are only three kinds of celestial objects in our model. In these situations, restricting the set of subclasses or subinterfaces can streamline the modeling. — JEP-397

# Warnings for value-based classes

[JEP-390](https://openjdk.java.net/jeps/390)

Primitive wrapper classes are value-based classes. Examples of value-based classes are:`Byte`,`Short`,`Integer`,`Long`,`Float`,`Double`,`Boolean`, and`Character`). More on value-based classes can be found [here](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/doc-files/ValueBased.html).

As they are immutable objects, it is pointless to use constructors.

To prevent misuse, new annotation is around. This will put warnings each time, the compiler finds a value-based class constructor.

`@jdk.internal.ValueBased`

# Encapsulation of JDK internals removed by default

[JEP-396](https://openjdk.java.net/jeps/396)

This is to encourage users to use standard Java API. Still, you could choose relaxed encapsulation. If needed, but try to avoid it.

You’ll need to add props to the launcher of JVM. Below you can see the possible args, you can pass in. More on the arguments can be found [here](https://openjdk.java.net/jeps/396#Description).

--illegal-access= permit | deny | debug | warn

Although this is embraced in JDK 16, you can face problems with earlier [versions](https://stackoverflow.com/questions/53790182/get-the-current-value-of-illegal-access-setting-in-java). This change was proposed after the JDK 9 [was released](https://jaxenter.com/jdk-9-replace-permit-illegal-access-134180.html).

> Even today, with e.g. Java &gt;=9, certain build tools print out [“reflective access”-warnings](https://issues.apache.org/jira/browse/GROOVY-8339)when building Java projects, which simply “feels not ready”, even though the builds are fine. — [source](https://www.marcobehler.com/guides/a-guide-to-java-versions-and-features)

# Pattern matching

[JEP-394](https://openjdk.java.net/jeps/394)

Pattern matching exists a long time. You can find it for example in Elixir.

In Java, there’s a lot of`instanceof`conditions. Motivation to reduce operations leads to pattern matching.

> Pattern matching allows the desired “shape” of an object to be expressed concisely (the_pattern_), and for various statements and expressions to test that “shape” against their input (the_matching_). — JEP-394

// a lot of boiler plate codeif (obj instanceof String) {  
String s = (String) obj;    // grr...  
...  
}

With pattern matching, we could get this code. Reduces boilerplate, does the casting, and declares the variable.

if (obj instanceof String s) {  
// Let pattern matching do the work!  
...  
}

Pattern variable is in the scope where it matches. Thus code like this is valid.

if (a instanceof Point p) {  
// p is in scope  
...  
}  
// p not in scope here  
if (b instanceof Point p) {     // Sure!  
...  
}

You never have to worry about names now. Reusing existing names is possible if scopes are different.

_What does pattern matching improve?_

Reduces explicit casts. Which gives more readable code, in equality methods.

return (o instanceof CaseInsensitiveString) &amp;&amp;  
((CaseInsensitiveString) o).s.equalsIgnoreCase(s);// to thisreturn (o instanceof CaseInsensitiveString cis) &amp;&amp;  
cis.s.equalsIgnoreCase(s);

Look at the next example. If this condition`o instanceof String s`, evaluates to true,`s`gets assigned a value. On the contrary,`s`has no value if pattern matching fails.

Thus this code can complete normally. You don’t have [an unreachable code](https://docs.oracle.com/javase/specs/jls/se15/html/jls-14.html#jls-14.22).`s`gets value assigned if conditional passes, if not`s`is safely discarded.

public void onlyForStrings(Object o) throws MyException {  
if (!(o instanceof String s))  
throw new MyException();// s has value at this point  
System.out.println(s);  
...  
}

# Conclusion

Java is evolving. Causes a lot of breaking changes. Causes a lot of [problems](https://medium.com/dev-genius/8-problems-every-java-developer-knows-4f65339e0c00).

![](https://miro.medium.com/max/700/0*MzIcNjgSR9uVS3yb.png)

[Constant Java updates](https://miro.medium.com/max/700/0*MzIcNjgSR9uVS3yb.png)

Even so, we do need to adjust. Use new features so we create better software.

These are few features, I would take away. You can read more, in the section below.

# Resources

[JEP draft: Pattern Matching for the switch (Preview)](https://openjdk.java.net/jeps/8213076)

[JDK 16: The new features in Java 16](https://www.infoworld.com/article/3569150/jdk-16-the-new-features-in-java-16.html)

Photo by [Christina Morillo](https://www.pexels.com/@divinetechygirl?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels)from[Pexels](https://www.pexels.com/photo/person-using-macbook-pro-on-person-s-lap-1181298/?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels)