Return to site

Java 26 — JEP 530: Primitive patterns and switch (Preview)

· java
int i = 1000;
if ( i instanceof byte b ) {
 System.out.println( "fits in byte: " + b );
}
boolean v = true;
switch ( v ) {
 case true -> System.out.println( "YES" );
 case false -> System.out.println( "NO" );
}

• Pattern matching now works with primitive types, not only reference types.

• The instanceof test can check whether a conversion is exact before binding a variable.

• switch also extends to primitive selectors such as boolean, long, float, and double.

#java #jdk26 #java26 #jep #jep530 #pathtojava27 #primitive #switch

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaBook👇