Question about type pattern inside record pattern

Cay Horstmann cay.horstmann at gmail.com
Fri Feb 6 16:00:36 UTC 2026


Consider this program:

record Amount(Number n) {}

Integer value(Amount p) {
     return switch (p) {
         case Amount(Integer value) -> value;
         case Amount(Number _) -> -1;
         case Amount(Object _) -> -2;
     };
}

void main() {
     IO.println(value(new Amount(null)));
}

It prints -1.

I have two questions:

1. Why does it compile? The case Amount(Object _) does not seem to reachable.
2. Why does null match case Amount(Number _) and not one of the other?

I tried applying JLS (for Java 25) §14.30, §15.28, and §14.11 but could not figure it out. Where should I look?

Thanks,

Cay

-- 

Cay S. Horstmann | https://horstmann.com



More information about the amber-dev mailing list