JEP 406 is a Java Enhancement Proposal (JEP) that was issued by the OpenJDK Community, the open-source organization responsible for developing the Java Development Kit (JDK). JEP 406 specifically proposes the addition of a new feature to the Java language called “pattern matching for switch,” which allows developers to use a more concise and readable syntax when checking the type of an object at runtime. The syntax for this feature is similar to the syntax used for pattern matching in other programming languages, such as functional languages like Haskell.

Here is an example of how the “pattern matching for switch” feature might be used:

Java 14 – Switch Expressions Share on X

Syntax –

if (obj instanceof String s) {
  // s is now a String
  System.out.println(s.length());
}

 

Example – If Else – before Java 17

public class JEP406_Example1 {
	
	public static void main(String[] args) {

		System.out.println(formatter(17));
		System.out.println(formatter(17L));
		System.out.println(formatter(17.0));
		System.out.println(formatter("Java 17"));

	}

	static String formatter(Object o) {
		String formatted = "unknown";
		if (o instanceof Integer i) {
			formatted = String.format("int %d", i);
		} else if (o instanceof Long l) {
			formatted = String.format("long %d", l);
		} else if (o instanceof Double d) {
			formatted = String.format("double %f", d);
		} else if (o instanceof String s) {
			formatted = String.format("String %s", s);
		}
		return formatted;
	}
}

 

Example – If Else – Java 17

public class JEP406_Example2 {

	public static void main(String[] args) {

		System.out.println(formatter(17));
		System.out.println(formatter(17L));
		System.out.println(formatter(17.0));
		System.out.println(formatter("Java 17"));
	}

	static String formatter(Object o) {
		return switch (o) {
		case Integer i -> String.format("int %d", i);
		case Long l    -> String.format("long %d", l);
		case Double d  -> String.format("double %f", d);
		case String s  -> String.format("String %s", s);
		default        -> o.toString();
		};
	}

}

 

Example – Pattern matching and null – before Java 17

public class JEP406_Example3 {
	
		  public static void main(String[] args) {

			  check("Tik");       // Dangerous
			  check("Tokker");    // Ok
			  check(null);        // Fail
			  check("");          // Ok
		  }

		  static void check(String s) {
		      if (s == null) {
		          System.out.println("Fail");
		          return;
		      }
		      switch (s) {
		          case "Tik", "Tok"   -> System.out.println("Dangerous");
		          default                     -> System.out.println("Ok");
		      }
		  }

 

Example – Pattern matching and null – Java 17

public class JEP406_Example4 {
	
	public static void main(String[] args) {

		  checkwithJava17("Tik");       // Dangerous
		  checkwithJava17("Tokker");    // Ok
		  checkwithJava17(null);        // Fail
		  checkwithJava17("");          // Ok
    }

    static void checkwithJava17(String s) {
        switch (s) {
            case null                   -> System.out.println("Fail");
            case "Tik", "Tok"           -> System.out.println("Dangerous");
            default                     -> System.out.println("Ok");
        }
    }

 

NOTE:

If you want to go deep into learning JEP 406 and want to see more examples, then please visit JEP 406: Pattern Matching for switch (Preview)

Java 14 – Switch Expressions Share on X
convert intstream to integer array

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts