The switch statement seems to be one of the best constructs that can use pattern matching. At present, a switch construct can match primitive literal values (excluding long, float, and double), String, and enum constants.
If a case label can specify a pattern, the code in the preceding section (the one that uses multiple instances of object checking and value extraction) can be modified as follows:
void dyingFish(Object obj) {
switch (obj) {
case Ocean o: System.out.println(o.getBottles());
break;
case Sea sea: System.out.println(sea.getDeadFish());
break;
case River riv: if (riv.getPlasticBags() > 100) {
System.out.println("Humans enjoy! Fish die!");
}
break...