Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Java 11 new Features (With example Programs)

  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
    Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth Biography
  • Ayodhya Kand in Hindi | अयोध्या काण्ड | राम को 14 वर्ष का वनवास
    Ayodhya Kand in Hindi | अयोध्या काण्ड | राम को 14 वर्ष का वनवास Spiritual
  • Fundamental Rights of Indian Citizens
    Fundamental Rights of Indian Citizens | मौलिक अधिकार क्या हैं? General Knowledge
  • Republic day गणतंत्र दिवस | Happy Republic Day
    Republic day गणतंत्र दिवस कब और क्यों मनाया जाता है? Festival
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
    Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी Biography
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
    Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi Biography
  • World Environment Day in Hindi : Objective, Importance, Theme
    World Environment Day in Hindi : Objective, Importance, Theme General Knowledge
  • States of Matter : Physical Properties of Matter (Solid, Liquid, Gas)
    States of Matter | Physical Properties of Matter (Solid, Liquid, Gas) Science

Java 11 new Features (With example Programs)

Posted on March 27, 2022May 4, 2022 By GeekCer Education No Comments on Java 11 new Features (With example Programs)

Several new features have been added to Java 11 to assist developers in avoiding boilerplate code and improving readability. Java 11 is the first Long Term Support (LTS) version following Java 8. Oracle discontinued supporting Java 8 in January 2019. Oracle, as we all know, releases a new Java version every six months.

Java is now the most widely used technology for application development. A web application or a desktop application can be used. However, the application owner must upgrade the application in order to maintain it up to date with the newest Java version.

Oracle no longer offers a free LTS for Java 11. The last free Oracle JDK release was Java 10. You can use OpenJDK and upgrade it whenever you want for free. If you want to use it on a enterprise scale, you’ll have to pay for support.

In this article we will know what are the important new features added in Java 11 or JDK 11?

Table of Contents

  • Running Java File with single command (Compile Free Run)
  • Local-Variable Syntax for Lambda (Var in Lambda)
  • New methods in String class
    • isBlank() in Java
    • lines() in Java
    • strip() in java
    • repeat(int) in java
  • File readString() and writeString() methods in Java
  • Convert Collections to Array in Java 11
  • Predicate.not() Method in Java
  • Java 11 Removed JMC and JavaFX features(module)
  • Epsilon garbage collector as Java 11 new Features

Running Java File with single command (Compile Free Run)

Earlier versions of Java required you to compile the source code with the javac command before running it with the java command.

However, with Java 11, you do not need to use the javac tool to compile the code. The capability to launch a file directly using java tool is included in Java 11.

If you don’t compile the code, Java 11 will explicitly compile it.


package com.geekcer;

public class CompileFreeLaunch {
	public static void main(String[] args) {
		System.out.println("Welcome to Java 11 Features!");
	}
}

>> java CompileFreeLaunch.java
Welcome to Java 11 Features!

Local-Variable Syntax for Lambda (Var in Lambda)

The var keyword is available in Java 11 and can be used with lambda arguments. To put it another way, we may use the var keyword in a lambda local variable.


package com.geekcer;

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

public class LambdaVarKeyword {
	public static void main(String[] args) {
		List list = Arrays.asList("Java", "C#", "C++", "HTML");

		String str = list.stream().map((var t) -> t).collect(Collectors.joining(", "));

		System.out.println(str);
	}
}

Output:
Java, C#, C++, HTML

New methods in String class

isBlank() in Java

The isBlank() method examines a string to see if it is empty or contains white space. This method returns a boolean value. The isBlank() method returns true if the string contains empty or white space otherwise returns false.


package com.geekcer;

public class JavaIsBlankDemo {
	public static void main(String[] args) throws Exception {
		String data = "";
		System.out.println(data.isBlank()); // Returns true

		String input = " ";
		System.out.println(input.isBlank()); // Returns true
		String value = "Java 11";
		System.out.println(value.isBlank()); // Returns false
	}
}

Output:
true
true
false

lines() in Java

The lines() method returns a collection of all line-splitting substrings. A stream of strings is what a collection is. When you have a multiline string, the lines() method is appropriate.


package com.geekcer;

import java.util.stream.Collectors;

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

		String multiLineString = "Program line1\nProgram line2\nProgram line3\nProgram line4";
		System.out.println(multiLineString);
		System.out.println(multiLineString.lines().collect(Collectors.toList()));
	}
}

Output:
Program line1
Program line2
Program line3
Program line4
[Program line1, Program line2, Program line3, Program line4]

strip() in java

In Java, the strip() method eliminates white space from the start and end of a string. In Java, the stripLeading() method eliminates white space from the start of a string. The stripTrailing() method eliminates white space from a string’s end.


package com.geekcer;

public class JavaStripDemo {
	public static void main(String[] args) {
		String inputString = " Java for Geek ";
		System.out.print("Before");
		System.out.print(inputString.strip());
		System.out.println("After");

		System.out.print("Before");
		System.out.print(inputString.stripLeading());
		System.out.println("After");

		System.out.print("Before");
		System.out.print(inputString.stripTrailing());
		System.out.println("After");
	}
}

Output:
BeforeJava for GeekAfter
BeforeJava for Geek After
Before Java for GeekAfter

repeat(int) in java

String.repeat(int) can be used to repeatedly concatenate strings. The repeat method repeats the string for the number of time specified.


package com.geekcer;

public class JavaRepeatDemo {
	public static void main(String[] args) throws Exception {
		String inputSting = "Java 11 for Geek | ".repeat(4);
		System.out.println(inputSting);
	}
}

Output:
Java 11 for Geek | Java 11 for Geek | Java 11 for Geek | Java 11 for Geek | 

File readString() and writeString() methods in Java

The ReadString() and WriteString() methods in Java 11 introduce a new concept for reading and writing files. These techniques save a lot of boilerplate code while also improves readability.


package com.geekcer;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class Java11FilesMethods {
	public static void main(String[] args) {
		try {
			Path filePath = Files.writeString(Path.of(File.createTempFile("file", ".txt").toURI()), "Java 11 Features",
					Charset.defaultCharset(), StandardOpenOption.WRITE);

			String fileData = Files.readString(filePath);

			System.out.println(fileData);
		} catch (IOException exception) {
			exception.printStackTrace();
		}
	}
}

Output:
Java 11 Features

Convert Collections to Array in Java 11

As we all know, there are several techniques to convert a collection to an array. However, with Java 11, there is a new mechanism to convert a Collection to an Array.

We’ll learn how to convert a Collection to an Array in Java 11 by using an example.


package com.geekcer;

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

public class CollectionsToArrayInJava {
	public static void main(String[] args) {
		List namesList = Arrays.asList("Java", "C#", "C++");
		String[] names = namesList.toArray(String[]::new);
		System.out.println(Arrays.toString(names));
	}
}

Output:
[Java, C#, C++]

Predicate.not() Method in Java

We can use the not() in Java 11 to negate an existing predicate. This works in the same way as the negate method.


package com.geekcer;

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

public class PredicateNegateDemo {
	public static void main(String[] args) {
		List countryList = Arrays.asList("INDIA", "UK", "SA", "USA");

		List countries = countryList.stream().filter(Predicate.not(String::isBlank))
				.collect(Collectors.toList());

		countries.forEach(country -> System.out.print(country + " "));
	}
}


Output:
INDIA UK SA USA

Java 11 Removed JMC and JavaFX features(module)

JMC (JDK Mission Control) and JavaFX modules are no longer accessible in Java 11. To use these modules, you must first download them.

Epsilon garbage collector as Java 11 new Features

Epsilon (no-operations(No-Op) garbage collector) is a new experimental feature in Java 11. It does not do collect garbage but does allocate memory. Out-of-memory faults are handled using a no-op garbage collector. The JVM will shut down when the Java heap is full.

To enable the Epsilon GC, type the following command.

-XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC

Some of its uses are listed below.

  • Performance evaluations
  • Pressure testing of memory
  • Extremely short-lived tasks and VM interface testing
  • Improvements in last-drop delay and throughput

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Important, Interview, Java Tags:Important new features of java 11, JDK 11 features, What changed in Java 11?

Post navigation

Previous Post: Difference between Runnable and Callable in Java
Next Post: DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार

More Related Articles

Difference between JPanel and JFrame Difference between JPanel and JFrame Differences
What is Mutual Fund? Mutual Fund and its types, Advantages and Disadvantages of Mutual Funds Important
Java Learning Tutorial Java Tutorial for Advanced Learning, Buzzwords, Parts of Java Java
What is Agriculture Agriculture (कृषि) से जुड़े महत्वपूर्ण प्रश्न और उत्तर Important
Stack Vs Heap in Java Stack Vs Heap in Java | Memory Allocation Section in Java Differences
Java 8 Stream map example | Java 8 map(function) parameter Java 8 Stream map example | Java 8 map(function) parameter Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved