The Amazing Features of Modern Java - Venkat Subramaniam

These notes are a summary of the following Youtube Video.

Venkat Subramaniam is a very informative speaker. I first saw him speak at a Spring Conference I attended in 2010.

Switch Expression

The Switch Expression was improved in newer versions of Java

return switch(input) {
	case 1 -> "one";
	case 2 , 3 -> "two or three";
	case 4 -> {
		yield "four";
	default -> "whatever"

The Switch Statement introduces a yield command that allows you to return a value from a switch statement.

The case statement no longer requires a break statement when used as an expression that returns or yields values, as shown by the example above.

Text Blocks

Java introduced this very similar feature to those provided in other languages.

String text = """
    Some Text Here
    And Here       \\s
	And Here


Text Blocks can span multiple lines and are surrounded by three quotes

Java Text Blocks are clever in that they recognize when -

  • the leading indent is not intentional and,
  • when leading indents are intentional, as shown in the third line in the example above.
  • the trailing space is not intentional, but can be made intentional by using a \\s

There is no need to escape characters in a Text Block

Text Blocks in Java are syntactic sugar for the compiler. The byte code reveals that these are compiled into a single String


A record is syntactic sugar used to implement objects and implements five methods that are hidden away.

  • Basic constructor
  • Getters
  • Equals method
  • Hash method
  • ToString method
Record Location(double lat, double lon) {}

var location1 = new Location(77,77);

Records are immutable. They do not come with setters.

The methods auto-implemented by a Record can be overridden. One use case would be to provide extra validation. Overriden constructors do not need to re-declare parameters.

Record Location(double lat, double lon) {
	public Location {
		if (lat > 99) {
			throw new RuntimeException("..");

Records are an easy way to create Tuples.

Records cannot be extended and are implicitly final. They can, however, implement interfaces.


A sealed interface allows you to limit how many classes implement the interface

sealed interface TrafficLight {}

final class Redlight implements TrafficLight {}
final class GreenLight implements TrafficLight {}
final class YellowLight implements TrafficLight {}

In the above example -

  • All classes in the same file implementing the interface and legitimate
  • A class in another file that attempts to implement the interface will get an error
sealed interface TrafficLight permits RedLight, GreenLight, YellowLight, FlashingLight;

Defining multiple final classes in a single file can get unwieldy. Therefore it is possible to specify the names of the classes that are permitted, as shown in the above example - using the permits keyword.

Pattern Matching

if (message instanceof String myMessage) {

The above example illustrates how it is possible to automatically cast a variable to another type using the instanceof keyword.

Extending Pattern Matching Into The Switch Statement

Pattern matching is possible when using the switch statement and it also supports a null check -

return switch(message){
	case null -> "you idiot"
	case Integer i -> "got a number"
	case String str -> "got a string of length " + str.length();
	default -> "whatever"

A guarded check ensures that the variable passed matches specific criteria. The Java Compiler will also now warn when a guarded check is unreachable, as is the case in the example below -