HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Keywords / yield keyword in Java

yield keyword in Java

Learn about yield keyword added in Java 14 to support switch expressions with example.

1. yield keyword

yield is added in Java 14, and is used inside switch expressions.

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; 
	}
};

System.out.println("Result is " + 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.
TwitterFacebookLinkedInRedditPocket

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 Keywords

  • Java Keywords
  • Java abstract
  • Java assert
  • Java boolean
  • Java final, finally, finalize
  • Java this, super
  • Java static
  • Java strictfp
  • Java synchronized
  • Java transient
  • Java yield

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

  • Sealed Classes and Interfaces