Pattern Matching for instanceof
Java 16 introduced Pattern Matching for instanceof, which provides a more concise and readable way to perform type checks and type casts. This feature allows developers to combine an instanceof check with a type cast in a single expression.
Example:
Before Java 16:
if (obj instanceof String) {
String str = (String) obj;
// ...
}
With Pattern Matching for instanceof:
if (obj instanceof String str) {
// ...
}
No comments:
Post a Comment