Java: The Evolution Switch (JEP 361)
Clean and Concise Switch Expressions

I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
Search for a command to run...
Clean and Concise Switch Expressions

I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
No comments yet. Be the first to comment.
Spring AI Tool Calling: From Chatbot to AI Agent with @Tool Your AI is smart. It knows an enormous amount. But it's frozen. It doesn't know what time it is right now. It doesn't know what's on your ca
Spring AI Advisors API Explained Series: Spring AI Complete Course — Lecture 4 of 12Reading Time: 8 minutesLevel: Intermediate Most developers stop at ChatClient. That is enough for demos. It is not
Working with Multiple AI Models in Spring AI Spring AI Complete Course — Lecture 3 of 12Previous: Lecture 2 — ChatClient API | Next: Lecture 4 — Advisors API In production AI applications, you rarel
Spring AI ChatClient API: The Fluent Heart of AI Integration Introduction If you've ever tried integrating AI models into a Java application, you know the pain. HTTP clients, API keys scattered everywhere, vendor-specific SDKs that never quite fit. W...
What is Spring AI? — Why Java Developers Need This in 2026 Every AI tutorial you see is in Python. LangChain, LlamaIndex, OpenAI SDK — all Python. But here's the uncomfortable truth: 80% of enterprise backends run Java. So who's building AI into thos...
Java switch expressions have been evolving with JDK release, while other JVM-based languages have been giving the compact and clean syntax, Java has waited as always and finally made the switch look a lot better.
Let's explore the Java switch expressions for now.
Look at the sample code below where switch expression will help to choose the breakfast based on the DAY of the week. Obviously, it is easy but has few pain points which make it error-prone and verbose.
enum DAYS {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
Now, let us write switch in the old way.
switch (DAY) {
case SATURDAY:
case SUNDAY:
System.out.println("It's Weekend Yay !");
System.out.println("Toast");
break;
case MONDAY:
System.out.println("Poha");
break;
case TUESDAY:
System.out.println("Oats");
break;
case WEDNESDAY:
System.out.println("Smoothie");
break;
case THURSDAY:
System.out.println("Dosa");
break;
case FRIDAY:
System.out.println("Bagel");
break;
}
Now, we will see how JEP 361 for Java 14 makes it easier and simple. The same switch can be written as below code
String breakFast = switch (DAY) {
case SATURDAY, SUNDAY -> {
System.out.println("It's Weekend Yay !");
yield "Toast";
}
case MONDAY -> "Poha";
case TUESDAY -> "Oats";
case WEDNESDAY -> "Smoothie";
case THURSDAY -> "Dosa";
case FRIDAY -> "Bagel";
};
yield rather return . This is done to avoid confusion whether it is the method return or the return from the switch blockbreak to terminate the execution of flow. It will only execute the code it is meant to execute by its arrow function