What is New in Java 13

Yiğitcan Nalcı
Javarevisited
Published in
4 min readDec 21, 2019

--

Greetings after a long break. In this post, I will tell you Text Blocks(JEP 355) and Switch Expressions(JEP 354) in Java 13.

Java 13

As you see, some other features in Java 13 are listed below. You can click this link for more information about these features.

  • Reimplement the Legacy Socket API — JEP 353
  • Dynamic CDS Archive — JEP 350
  • ZGC: Uncommit Unused Memory — JEP 351
  • FileSystems.newFileSystem() Method
  • Support for Unicode 12.1
  • DOM and SAX Factories with Namespace Support?

How to enable support for Java 13 preview features?

Text Blocks(JEP 355) and Switch Expressions(JEP 354) are preview features for Java 13. You can use these features by enabling the preview-feature settings in your project.

Photo by Markus Spiske on Unsplash

If you are running java program from the command line, you can enable it using the --enable-preview switch. You can use this switch to start JShell with preview features enabled.

$ jshell --enable-preview  
$ java --enable-preview --source 13 Test.java

If you are using IntelliJ IDEA, you have to set the corresponding Language Level in the Project Structure settings and install the JDK to compile.

If you are using Eclipse, you can enable the preview features from the Java Compiler settings.

Text Blocks

Multiline strings can be created easily with this preview feature. The multiline string has to be written inside a pair of triple-double quotes.

You can create these text blocks using just these quotes without the need for anything else.

public class TextBlocks {

/**
* JEP 355: Preview Feature
*/
@SuppressWarnings("preview")
public static void main(String[] args) {
String textBlock = """
Hello
Java13
New Feature""";

String str = "Hello\nJava13\nNew Feature";

System.out.println("Text Block String:\n" + textBlock);
System.out.println("Normal String:\n" + str);

System.out.println("Text Block and String Literal equals() Comparison: " + (textBlock.equals(str)));
System.out.println("Text Block and String Literal == Comparison: " + (textBlock == str));
}

}

Output:

Text blocks output

Using text blocks in Java 13, also you can use new methods in String class. For example, formatted(Object... args), stripIndent(), translateEscapes().

Switch Expressions

With Java 12 release, switch expressions were added as a preview feature. I have already mentioned here how to use them in Java 12.

Java 12 release is almost same in Java 13 except yield . With Java 13, the break statement has been replaced with yield .

public class SwitchExpressions {

@SuppressWarnings("preview")
public static void main(String[] args) {
Day day = Day.WEDNESDAY;

switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
System.out.println(6);
break;
case TUESDAY:
System.out.println(7);
break;
case THURSDAY:
case SATURDAY:
System.out.println(8);
break;
case WEDNESDAY:
System.out.println(9);
break;
default:
System.out.println(-1);
}

// from java 13 onwards - multi-label case statements
switch (day) {
case MONDAY, FRIDAY, SUNDAY:
System.out.println(6);
break;
case TUESDAY:
System.out.println(7);
break;
case THURSDAY, SATURDAY:
System.out.println(8);
break;
case WEDNESDAY:
System.out.println(9);
break;
default:
System.out.println(-1);
}

// switch expressions, use yield to return, in Java 12 it was break
int x = switch (day) {
case MONDAY, FRIDAY, SUNDAY:
yield 6;
case TUESDAY:
yield 7;
case THURSDAY, SATURDAY:
yield 8;
case WEDNESDAY:
yield 9;
default:
yield -1;
};
System.out.println("x = " + x);

}

enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}

Output:

Switch expressions output

Thank you for reading! 🙏 Your thoughts are very valuable to me. Please feel free to share. 😄

--

--

Yiğitcan Nalcı
Javarevisited

Dad. Software Engineer. Sports lover, doer. Curious about science and technology. Novice writer for now. https://www.linkedin.com/in/yigitcannalci