Return to site

☕ JAVA TRICK: Boolean expression in Pattern Matching

December 3, 2021

You know pattern matching, but did you know that you can use the variable it introduces in the same boolean expression?

If you check whether the instance 'ðĻ𝐛ðĢ𝐞𝐜𝐭' is of type '𝐒𝐭ðŦðĒ𝐧𝐠' with 'ðĻ𝐛ðĢ𝐞𝐜𝐭 ðĒ𝐧𝐎𝐭𝐚𝐧𝐜𝐞ðĻ𝐟 𝐒𝐭ðŦðĒ𝐧𝐠 𝐎',

you can start using '𝐎' straight away, for example to check whether it's non-empty with '&& !𝐎.ðĒ𝐎𝐄ðĶðĐ𝐭ðē()'.

This works in 'ðĒ𝐟' statements in 𝐉𝐚ðŊ𝐚 𝟏𝟔 and in '𝐎𝐰ðĒð­ðœðĄ' as a preview in 𝐉𝐚ðŊ𝐚 𝟏𝟕.