Make writing a habit together! This is the 7th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Java8 was released on March 18, 2014. As of April 6, 2022, the current latest release is Java18. Versions 17, 11, and 8 are the long Term Support (LTS) versions currently supported. This article takes you through the features of each version of Java from 8 onwards. Sit back and go! Want to have a crush on an article, click here to interview the kill | please discuss Java8-18 of the new features introduced (a)

New features in Java 9

Stream.ofNullable. Starting with Java 8, we can use stream. of (Object… Values) creates a Stream of elements. From Java 9 we can use stream. ofNullable on individual elements to create a Stream or return an empty Stream.

List<Location> locList = new ArrayList<>(Arrays.asList(loc, loc1, loc2));
Stream.ofNullable(locList);
Copy the code

Private Methods in Interface

Java 8 introduced the concept of default methods in interfaces. If you have multiple default methods that want to share the same logic, you can use private methods to implement the public logic.

Java Modules

A Java module is a technique in which packages and their associated resources can be combined. A module contains a module descriptor, which specifies the module name, dependent modules, packages available to other modules, and so on.

One of the ultimate goals is to reduce the size of your application at run time. If we don’t use a GUI, we don’t need to add this module as a dependency in our application.

There are other new features, including but not limited to:

  • Try-With Resources
  • Anonymous Classes
  • @SafeVarargs Annotation
  • Collection Factory Methods
  • New Version-String Scheme
  • JShell: The Java Shell (REPL)
  • Installer Enhancement for Microsoft windows and many more

New features in Java 10

local variable type inference

Starting with Java 10, we can initialize non-empty local variables and assign them to the var type. The type of a variable is determined by context.

Examples are as follows:

public class Sample { public static void main(String[] args) { var x = 10; var y = 11; System.out.println(x + y); var obj = new TryVar(); System.out.println(obj.getName()); var c; //Illegal, Compiler will throw an error -> "Cannot use 'var' on variable without initializer" } } class TryVar { String name; public String getName() { return "my name"; }}Copy the code

copyOf()

List, java.util. Map, and java.util. Set each have a new static method copyof (collection).

It returns a non-executable copy of the given collection:

@Test(expected = UnsupportedOperationException.class)
public void whenModifyCopyOfList_thenThrowsException() {
    List<Integer> copyList = List.copyOf(someIntList);
    copyList.add(4);
}
Copy the code

Any attempt to modify such collection will result in Java. Lang. UnsupportedOperationExceptionRuntime anomalies.

There are other new features, including but not limited to:

  • Performance Improvements
  • toUnmodifiable*()
  • Unmodifiable Collections
  • Container Awareness

New features in Java 11

Java 11 as the LTS version

New String Methods

Java 11 added new methods to the String class: isBlank, lines, Strip, stripLeading, stripTrailing, and repeat.

Extract a non-blank string from a multi-line string:

String multilineString = "Baeldung helps \n \n developers \n explore Java."; List<String> lines = multilineString.lines() .filter(line -> ! line.isBlank()) .map(String::strip) .collect(Collectors.toList()); assertThat(lines).containsExactly("Baeldung helps", "developers", "explore Java.");Copy the code

New File Methods

Now that it’s easier to read and write strings from Files, you can use the new readString and writeString static methods in the Files class.

Path filePath = Files.writeString(Files.createTempFile(tempDir, "demo", ".txt"), "Sample text");
String fileContent = Files.readString(filePath);
assertThat(fileContent).isEqualTo("Sample text");
Copy the code

Collection to an Array

The Collection interface contains a new default toArray method that takes an IntFunction argument. This makes it easier to create arrays of the right type from collections:

List sampleList = Arrays.asList("Java", "Kotlin");
String[] sampleArray = sampleList.toArray(String[]::new);
assertThat(sampleArray).containsExactly("Java", "Kotlin");
Copy the code

There are other new features, including but not limited to:

  • Running Java File with single command
  • Local-Variable Syntax for Lambda Parameters
  • Nested Based Access Control
  • JEP 321: HTTP Client
  • JEP 328: Flight Recorder

To continue, the following will continue to talk about the new features of each version, stay tuned!