Skip to content

Instantly share code, notes, and snippets.

@enikao
enikao / AssertPatternMatching.java
Created November 23, 2022 20:41
Java Combining assert statement and instanceof pattern matching
public class AssertPatternMatching {
public static void main(String[] args) {
Object obj = args.length == 0 ? Integer.valueOf(42) : "Hello";
afterAssert(obj);
insideMessage(obj);
}
private static void afterAssert(Object obj) {
assert obj instanceof String str;