Estimate project
← All posts

Java 27: Primitive Types in Patterns, Instanceof, and Switch

See how Java 27 brings primitive types to instanceof and switch, making code easier to read

4 min read
Javauncoverjavapattern matchingswitch

Every six months, a new, fresh and tasty Java version lands. This time it’s Java 27, and in this article we’ll go over JEP-532, which brings primitive types to patterns, instanceof and switch. Keep in mind that it’s a preview feature.

The JEP’s title may seem to say it all, but it pays to dig a little deeper.

Note: This article first appeared in September 2024. I revised it in September 2026 to match the Java 27 release, so it reflects the newest changes and improvements that came with that version.

Before Java 25

Let’s see what things looked like before this JEP. Working with switch or instancesof came with considerable limitations. In a switch, not every boxed type could be used.

If you tried writing this code,

Double d = 20.0d;
switch(d){
   case 20.0d -> log("double is 20");
   default -> log("wrong number" + d);
}

you would get this compilation error:

java: constant label of type double is not compatible with switch selector type java.lang.Double

The reason is that certain types were restricted. The JLS states:

“This Expression is referred to as the selector expression. Its type has to be char, byte, short, int or a reference type; otherwise, a compile-time error is raised.”

The quote tells us that the int type can be used in a switch. Indeed, you can write code such as:

int v = 20;
switch(v){
   case 20 -> log("int is 20");
   default -> log("wrong number" + v);
}

and it all works fine. As you may know, switch received many improvements in Java 21, letting you write something like:

Integer v = 20;
switch(v){
   case Integer i when i < 20-> log("This value is too low: " + i );
   case Integer i -> log("This value is perfect: " + i );
}

Nice, isn’t it? Let’s attempt the same with a primitive int.

int v = 20;
switch(v){
   case int i when i < 20 -> log("This value is too low: " + i );
   case int i -> log("This value is perfect: " + i );
}

Running this code ends in a compilation error:

java: unexpected type

  required: class or array

  found:    int

What’s the reason? Until JEP-507, switch patterns were only partially supported in Java.

Such Switch Patterns could be used with Record Patterns.

record Value(int i){}
void test(){
   Value v = new Value(20);
   switch (v) {
       case Value(int i) when i < 20 -> log("This value is too low: " + i );
       case Value(int i) -> log("This value is perfect: " + i );
   }
}

This works as expected. Instanceof, meanwhile, didn’t support any primitive type at all.

After Java 25 - With the preview-feature enabled

A switch now accepts Long, Float, Double and Boolean, so the earlier example with Double works.

Instanceof behaves differently too: you can now check whether a variable is of a primitive type.

From now on, running code like this:

int i = 42;
if(i instanceof int){
   log("i is an instance of int");
}

prints “i is an instance of int” to the console. Excellent! And what about this code?

int i = 42;
if(i instanceof byte){
   log("i is an instance of byte?");
}

Simple enough, right? Nothing gets printed, since an int isn’t an instance of byte. Except that, in this example, we do get the text “i is an instance of byte?”

Why is that?

It all comes down to casting – the basic Java mechanism of assigning a value of one primitive data type to another type.

Did you know casting isn’t safe? Take a look at this example:

int i = 42;
byte b = (byte) i;
log("b value is " + b);

Our b variable will hold 42, correct? And now?

int i = 128;
byte b = (byte) i;
log("b value is " + b);

You likely sense there’s a catch in this question, and you’re right: b would be -128. This happens because the byte type covers the range from -128 to 127, inclusive. You can read more about the Integral Types and Values.

This raises a question: if casting changes the value, is it the right type? According to the JEP, it isn’t.

This is why instanceof now not only verifies that the type is correct for references, but for primitive values also checks whether the value can be cast safely.

What does this lead to?

Before Java 25, trying code like this:

Integer i = 42;
switch (i) {
   case Double d -> log("i is " + d); 
}

resulted in an error:

error: incompatible types: Integer cannot be converted to Double

With primitives, however, code like this:

int i = 42;
switch (i) {
   case double d -> log("i is " + d)
}

works without issues, since any int can be cast to double safely. If you try the same with narrower types, your switch has to be exhaustive, like this:

int i = 10_0000;
switch (i) {
   case short v -> log("i is " + v);
   default -> log("not in scope of short" + i);
}

To find out whether casting a primitive type to another one is safe, check Casting Contexts in JLS.

Summary

Switch patterns keep getting more useful in everyday work. Project Amber now lets us handle primitives more easily and with less verbose methods.