//Before Pattern Matching if (obj instanceof String) { String str = (String) obj; System.out.println(str); } //After Pattern Matching if (obj instanceof String str) { System.out.println(str); }