Whats new in Java 11

You can follow the latest updates in Java here: Java-version.com

Some of the new features in Java 11

In this page we will list some of the new features of Java 11 (from 9 to 11) relevant for the daily activity of business application developers.

Local-variable type inference

With Java 11 is possible to declare local variable with type inference with initializer (keyword var).

// words is of type String[] 
var words = new String[] {"abc", "def"}; 
Assertions.assertTrue(words instanceof String[]); 
// the initializer is mandatory 
// dataList is of type ArrayList 
var dataList = new ArrayList(); 
Assertions.assertEquals("abc", dataList.get(0)); 
Assertions.assertEquals("def", dataList.get(1)); 

##Features in the String class
A new method lines() creates a Stream with the lines of the String.

public void stringCountTest() { 
  String text = "line 1\nline 2\nline3"; 
  // the new lines method return a Stream 
  assertTrue(text.lines() instanceof Stream); 
  // each element in the stream represents a line of the input string 
  assertEquals(3, text.lines().count()); 

strip - stripLeading - stripTrailing
strip eliminates the whitespaces at the beginning and at the end of a String.

public void stripTest() { 
  String example = "      leading and trailing spaces      "; 
  assertEquals("leading and trailing spaces", example.strip()); 
  assertEquals("leading and trailing spaces      ", example.stripLeading()); 
  assertEquals("      leading and trailing spaces", example.stripTrailing()); 

isBlank() returns true if the String is empty or it contains only whitespaces.

public void isBlankTest() { 
  assertTrue("     ".isBlank()); 

repeat composes a string repeating its content for a defined number of times.

public void repeatTest() { 
  assertEquals("abc abc abc ", "abc ".repeat(3)); 
  assertEquals("", "abc ".repeat(0)); 


Optional get a new method to check if the value is not present: isEmpty()

public void isEmptyTest() { 
  Optional<Integer> evaluate = Optional.of(5); 

WebApp built by Marco using SpringBoot, Java 17, Mustache, Markdown and in Azure