import java.util.function.*; import org.jetbrains.annotations.Nullable; public class InstanceOfPattern { void test2(@Nullable Foo foo) { if (Math.random() > 0.5 && foo.bar() instanceof String s) { System.out.println(s.length()); } if (Math.random() > 0.5 && foo.getBar() instanceof String s) { System.out.println(s.length()); } } void test(Foo foo) { if (foo.bar() instanceof String s) { System.out.println(s.length()); } } void test(Object obj) { if (obj instanceof Number n) { if (n == obj) {} } } void testNullCheck(String s) { if (s instanceof String s1) { System.out.println(s1); } } void testNullCheckUnusedPatternVariable(String s) { if (s instanceof String s1 && s1.length()==2) { System.out.println("foo"); } } interface Foo { @Nullable Object getBar(); @Nullable Object bar(); } }