Better HN
topnewbestaskshowjobs
Log inSign up

GavinBierman

1 karma · joined May 16, 2022 (4y ago) · 1 submissions

Submissions

comment 4y ago · link · parent

This is all true, but with pattern matching for instanceof you don't need the explicit cast:

    Object foo = ...
    if (foo instanceof String s) {
        System.out.println(s.toLowerCase())
    }
In this case, the scope of the pattern variable `s` is just the then block, so are free to reuse it elsewhere in the block.