Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B38FGKKam5SCGgwgduVAh3
29 lines
1.2 KiB
Plaintext
29 lines
1.2 KiB
Plaintext
$ java --enable-preview PrimitivePatterns (JDK 27)
|
|
--- instanceof <primitive>: an exact-conversion test, not a cast
|
|
100 instanceof byte = true
|
|
300 instanceof byte = false (a byte holds -128..127)
|
|
CHECK ok : 100 converts to byte exactly, 300 does not
|
|
binding: byte narrow = 100
|
|
16_777_216 instanceof float = true
|
|
16_777_217 instanceof float = false (float has a 24-bit significand)
|
|
CHECK ok : int to float is exact up to 2^24 and lossy after
|
|
--- switch on a long, with pattern labels and a guard
|
|
classify(0) = zero
|
|
classify(1) = one
|
|
classify(-7) = negative (-7)
|
|
classify(9000000000) = positive (9000000000)
|
|
CHECK ok : long switch dispatches on constants, then patterns
|
|
--- switch on an Object: an Integer matches 'case int'
|
|
describe(Integer 7) = an int 7
|
|
describe(Integer 5000) = a large int 5000
|
|
describe(Long 7) = a long 7
|
|
describe(Double 2.5) = a double 2.5
|
|
describe(String hello) = a String of length 5
|
|
describe(Character x) = something else: x
|
|
CHECK ok : an Integer 5000 matches case int with a guard
|
|
--- record pattern with a narrowing primitive component
|
|
Reading[celsius=36] -> fits in a byte: 36
|
|
Reading[celsius=4000] -> needs a full int: 4000
|
|
CHECK ok : Reading(byte c) matches 36 but not 4000
|
|
exit=0
|