Master java skills

Java 11 Features

Java 11 has introduced many new features. Let’s have a look at some important points

  1. Java 10 was the last free Oracle JDK release that could be used commercially without a license. But with Java 11 onwards, there’s no free long-term support (LTS) from Oracle. But Oracle continues to provide Open JDK releases that can be used by the developers.
  2. Auto-update has been removed from JRE installations in Windows and macOS.
  3. Java language translation for French, German, Spanish, Portuguese, Korean and Swedish is no longer provided.
  4. JRE or Server JRE is no longer offered. Only JDK is offered from java 11 onwards
  5. The deployment stack required for running applets and web applications has been removed from JDK that was deprecated in JDK 9.

Development features of Java 11

1. New String Methods

Many new string methods have been introduced in java 11.

1.1) isBlank(): This method is used to check whether a particular string is empty or not.

package com.javatrainingschool;

class BlankStringExample {
	
	public static void main(String args[]) {
		String str1 = "";
		System.out.println("str1 is blank : " + str1.isBlank());

		String str2 = "Java Training School";
		System.out.println("str2 is blank : " + str2.isBlank());
	}
	
}

Output:

1.2) lines() method

This method returns a collection of strings that are divided by line terminators.

package com.javatrainingschool;

import java.util.List;
import java.util.stream.Collectors;

public class LinesExample {

	public static void main(String args[]) {
		String str = "Java \n Training \n School";
		List<String> lines = str.lines().collect(Collectors.toList());
		System.out.println(lines);
	}
}

1.3) repeat(n): This method is used to concatenate a string multiple times. The no of times it should be concatenated is decided by the number passed as an argument to this method.

package com.javatrainingschool;

public class RepeatExample {

	public static void main(String args[]) {
		String str = "Hello world!";
		System.out.println(str.repeat(3));
	}
	
}

1.4) stripLeading(), stripTrailing, and strip() method

These methods are used to remove white spaces from front, back and front-back both respectively.

package com.javatrainingschool;

public class StripExample {

	public static void main(String args[]) {
		String str = " Hello world! ";
		System.out.println("StripLeading : " + str.stripLeading());
		System.out.println("StripTrailing : " + str.stripTrailing());
		System.out.println("Strip : " + str.strip());
	}

}

2. New File Methods

2.1) Files.readString() method – This method is used to read a file

package com.javatrainingschool;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileReadingExample {

	public static void main(String[] args) throws IOException {

		// we can read the content of the file by Files.readString method
		String fileContent = Files.readString(Path.of("C:\\Users\\Hp\\Documents\\Java_Programs\\test.txt"));
		System.out.println(fileContent);

	}
}

2.2) Files.write() method – This method is used to write a file. Please note that already written content will be overridden using this method

package com.javatrainingschool;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileWritingExample {

	public static void main(String[] args) throws IOException {
		
		Files.writeString(Path.of("C:\\Users\\Hp\\Documents\\Java_Programs\\test.txt"), "Hello champion!");
		
		System.out.println("File written successfully.");

		String fileContent = Files.readString(Path.of("C:\\Users\\Hp\\Documents\\Java_Programs\\test.txt"));
		
		System.out.println(fileContent);
		
	}
}

2.3) isSameFile(): This method is used to know whether two paths locate the same file or not.

package com.javatrainingschool;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileReadingExample {

	public static void main(String[] args) throws IOException {
		
		Path path1 = Path.of("C:\\Users\\Hp\\Documents\\Java_Programs\\test.txt");
		Path path2 = Path.of("C:\\Users\\Hp\\Documents\\Java_Programs\\test.txt");
		
		boolean result = Files.isSameFile(path1, path2);
		
		System.out.println(result);
	
	}
}

Output:

True

3. Collection to an Array

A new default method toArray which takes an IntFunction argument has been added to The java.util.Collection interface. This makes it easier to create an array from a collection:

package com.javatrainingschool;

import java.util.Arrays;
import java.util.List;

public class ListToArrayExample {

	public static void main(String[] args) {
		
		List<String> names = Arrays.asList("Arjun","Shankar","Javed");
		String [] namesArr = names.toArray(String[] :: new);
		
		for(String name : namesArr) {
			System.out.println(name);
		}		
	}
}

4. Pattern matching methods

asMatchPredicate method is added.

package com.javatrainingschool;

import java.util.function.Predicate;
import java.util.regex.Pattern;

public class PatternExample {

	public static void main(String[] args) {
		
		Predicate<String> p = Pattern.compile("xyzz").asMatchPredicate();
		
		System.out.println(p.test("xyzz"));
		
	}
}
Output:
true

5. Not predicate method

A static method called ‘not’ has been added to Predicate interface. We can use it to negate an existing predicate like negate method.

package com.javatrainingschool;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class NotPredicateExample {
	
	public static void main(String[] args) {
		
		List<String> list = Arrays.asList("Hello", "  ", "programming", "   ");
		list.forEach(e -> System.out.println("element : " + e));
		
		//using not to exclude the objects
		List<String> listWithoutBlanks = list.stream()
		  .filter(Predicate.not(String::isBlank))
		  .collect(Collectors.toList());
		
		listWithoutBlanks.forEach(e -> System.out.println("element after : " + e));
		
	}
}

6. Local variable syntax for lambda expression

JDK 11 allows ‘var’ to be used in lambda expressions. This was introduced to be consistent with the local ‘var’ syntax of Java 10.

package com.javatrainingschool;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Example {

	public static void main(String[] args) {
		Stream.of("Mark", "David", "Joe").map((var name) -> name.toUpperCase())
		                .collect(Collectors.toList()).forEach(System.out::println);
	}
}

7. Epsilon Garbage Collector

Java 11 introduced a No-Op Garbage Collector called Epsilon. This GC promises the lowest GC overhead possible. This handles memory allocation but does not have an actual memory reclamation mechanism. Once the available Java heap is exhausted, JVM shuts down. 

The main goals of Epsilon Garbage Collector are :

  1. Performance Testing
  2. VM interface testing
  3. Extremely short lived jobs
  4. Memory pressure testing
  5. Last drop latency improvements
  6. Last drop throughput improvements

8. Removed thread functions

The two methods stop(Throwable obj) and destroy() have been removed from the JDK 11.

9. Removal of Java EE and CORBA modules

These modules were deprecated in Java 9 with a declaration to remove those in further JDK versions. In java 11 it has been removed.

10. Time Unit Conversion

Using TimeUnit class’ convert method, we can change time to Days, Months, Years etc. Consider below example

package com.javatrainingschool;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class TimeUnitExample {

	public static void main(String[] args) {
		
		TimeUnit tu = TimeUnit.DAYS;
		
		int noOfHours = 96;
		
		long t = tu.convert(Duration.ofHours(noOfHours));
		
		System.out.println(noOfHours + " hours are " + t + " days");
		
	}	
}

11. Optional.isEmpty() method

This is used to check if the value of the object is null or not. If null, it returns true otherwise false

package com.javatrainingschool;

import java.util.Optional;

public class OptionalExample {

	public static void main(String[] args) {
		
		Optional value = Optional.empty();
		
		System.out.println(value.isEmpty());
		
		value = Optional.of("Ram");
		
		System.out.println(value.isEmpty());
		
	}	
}

12. Running java files without compilation command

A major change in java 11 is that we can run java programs without running the compilation command javac using javac.

Before java 11, if we wanted to run a java class called HelloWorld.java, then we would do this

But going forward with java 11, we can run the same program using below command. The point to note here is that we need to use .java extension with java command unlike before.

13. Removed features in java 11

Below is the list of features removed in java 11

SrFeature Removed
1Java Deployment Technologies
2JMC from the Oracle JDK
3JavaFX from the Oracle JDK
4JEP 320 Remove the Java EE and CORBA Modules
5appletviewer Launcher
6com.sun.awt.AWTUtilities Class
7Lucida Fonts from Oracle JDK
8Oracle JDK’s javax.imageio JPEG Plugin No Longer Supports Images with alpha
9sun.misc.Unsafe.defineClass
10Thread.destroy() and Thread.stop(Throwable) Methods
11sun.nio.ch.disableSystemWideOverlappingFileLockCheck Property
12sun.locale.formatasdefault Property
13JVM-MANAGEMENT-MIB.mib
14SNMP Agent

14. Deprecated features in java 11

SrFeatures Deprecated
1Obsolete Support for Commercial Features
2JEP 335 Deprecate the Nashorn JavaScript Engine
3ThreadPoolExecutor Should Not Specify a Dependency on Finalization
4Deprecate -XX+AggressiveOpts
5Stream-Based GSSContext Methods are also deprecated
6JEP 336 Deprecate the Pack200 Tools and API