HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 14 / Java 14 – switch expression

Java 14 – switch expression

A switch statement allows the application to have multiple possible execution paths based on the value of a given expression in runtime.

The evaluated expression is called the selector expression which must be of type char, byte, short, int, Character, Byte, Short, Integer, String, or an enum.

In Java 14, with switch expression, the entire switch block “gets a value” that can then be assigned to a variable in same statement.

1. switch expression example

  • In Java 14, it is a standard feature. In Java 13 and Java 12, it was added as an preview feature.
  • It has the support of multiple case labels and using yield to return value in place of old return keyword.
  • It also support returning value via label rules (arrow operator similar to lambda).
  • If we use arraw (->) operator, we can skip yield keyword as shown in isWeekDayV1_1().
  • If we use colon (:) operator, we need to use yield keyword as shown in isWeekDayV1_2().
  • In case of multiple statements, use curly braces along with yield keyword as shown in isWeekDayV2().
  • In case of enum, we can skip the default case. If there is any missing value not handled in cases, compiler will complain. In all other expression types (int, strings etc), we must provide default case as well.
public class SwitchExpressions 
{
	public static void main(String[] argv) 
	{
		System.out.println(isWeekDayV1_1(Day.MON));		//true
		System.out.println(isWeekDayV1_2(Day.MON));		//true
		System.out.println(isWeekDayV2(Day.MON));		//true
	}
	
	//1 - Return value directly
	
	enum Day {
		MON, TUE, WED, THUR, FRI, SAT, SUN
	};
 
	public static Boolean isWeekDayV1_1 (Day day) 
	{
		Boolean result = switch(day) {
			case MON, TUE, WED, THUR, FRI -> true;
			case SAT, SUN -> false;
		};
		return result;
	}
	
	public static Boolean isWeekDayV1_2 (Day day) 
	{
		Boolean result = switch(day) {
			case MON, TUE, WED, THUR, FRI : yield true;
			case SAT, SUN : yield false;
		};
		return result;
	}
	
	//2 - Multiple statements
	
	public static Boolean isWeekDayV2 (Day day) 
	{
		Boolean result = switch(day) {
			case MON, TUE, WED, THUR, FRI ->
			{ 
				System.out.println("It is WeekDay");
				yield true; 
			}
			case SAT, SUN ->
			{ 
				System.out.println("It is Weekend");
				yield false; 
			}
		};
		return result;
	}
}

2. yield vs return

A return statement returns control to the invoker of a method or constructor. A yield statement transfers control by causing an enclosing switch expression to produce a specified value.

SwitchExpression:
	YieldStatement:
    	yield Expression;
  • SwitchExpression tries to find a correct YieldStatement to transfer control to innermost enclosing yield target.
  • SwitchExpression terminates normally and the value of the Expression becomes the value of the SwitchExpression.
  • If the evaluation of the Expression completes abruptly for some reason, then the yield statement completes abruptly for same reason.

Drop me your questions in comments related to enhanced switch statement in Java 14.

Happy Learning !!

Sourcecode Download

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java 14 Tutorial

  • Java 14 – New Features
  • Java 14 – record types
  • Java 14 – Text blocks
  • Java 14 – Helpful NPE
  • Java 14 – switch expression
  • Java 14 – yield keyword
  • Java 14 – instanceof

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)