Object value = "some text";
// *** before: ***
if (value instanceof String) {
String s = (String) value; // the type is written three times
System.out.println(s.length());
}
// *** in version 16: ***
if (value instanceof String s) { // the check and the variable in one
System.out.println(s.length());
}
// the variable lives where the check has already succeeded:
if (value instanceof String s && s.length() > 2) {
System.out.println(s.toUpperCase());
}
if (!(value instanceof String s)) {
System.out.println("not a string");
} else {
System.out.println(s.strip());
}