instanceof
expressions, making conditions easier to read and understand.instanceof
: Seamlessly integrates with existing instanceof
checks, allowing for conditional actions based on record content in a type-safe manner.Before Java 21:
Before, you had manually cast an object after asserting it was possible with an instanceof
operation.
for (Shape shape : shapes) { if(shape instanceof Circle) { Circle c = (Circle) shape; System.out.printf("Circle with radius %.1f", c.radius()); } else if(shape instanceof Rectangle) { Rectangle r = (Rectangle) shape; System.out.printf("Rectangle with width %.1f and height %.1f", r.width(), r.height()); } System.out.printf(" and area %.1f and perimeter %.1f\n", shape.calcArea(), shape.calcArea()); }
After Java 21:
With the new pattern matching, the use of instanceof
immediately automatically casts the object to that type and immediately makes its components available for local use.
for (Shape shape : shapes) { if(shape instanceof Circle(double radius)) { System.out.printf("Circle with radius %.1f", radius); } else if(shape instanceof Rectangle(double width, double height)) { System.out.printf("Rectangle with width %.1f and height %.1f", width, height); } System.out.printf(" and area %.1f and perimeter %.1f\n", shape.calcArea(), shape.calcArea()); }
Notice how we don’t have to call the properties via the object, we have immediate access to those values.
Pet
-interface with an implementing Cat
-class according to the UML provided..main
-method of a PetsApp
-class, make an array of pets.processPet
-method that accepts a Pet
-object and, if it’s a cat, prints its name and age.main
-method, have each of your Cat
-objects in your array processed using the processPet
-method.printPetJson
-method which accepts a pet, and prints it in JSON-format.name
component to solve this.public class PetApp { public static void main(String[] args) { Pet[] pets = { new Cat("Fluffy", 2), new Cat("Picasso", 7), new Cat("Dorado", 1) }; Arrays.stream(pets) .peek(PetApp::processPet) .forEach(PetApp::printPetJson); } private static void processPet(Pet pet) { if (pet instanceof Cat(String name, int age) && age < 2) { System.out.println(name + " is still just a kitty!"); } } private static void printPetJson(Pet pet) { if (pet instanceof Cat(String name, int age) && name != null) { System.out.printf(""" { "name": "%s", "age": %d } """, name, age); } else { System.out.println("Unsupported pet type"); } } }
public interface Pet { }
public record Cat(String name, int age) implements Pet { }
instanceof
: Seamlessly works with instanceof
checks, enabling conditional actions and immediate access to component values in a type-safe way, thus avoiding additional property calls.