Nearby lessons

116 of 125

Java 11 Features

📌 What You Will Learn
  • Why Java 11 is an important LTS version
  • The new String methods: isBlank, strip, repeat, lines
  • var in lambda parameters
  • Running a single Java file directly
  • The new HttpClient

Java 11 Features is a core concept of the Java language. This lesson explains Java 11 — A Long-Term Support (LTS) Version, New String Methods and strip() vs trim() — Small but Important with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Java 11 — A Long-Term Support (LTS) Version

Java 11 (September 2018) is a Long-Term Support (LTS) version — it gets updates and security patches for years. Companies trust Java 11 (along with 8, 17 and 21). It also cleaned up the platform by removing old, unused things like the Java EE modules and the JavaFX bundling.

New String Methods

Java 11 gave String several handy methods that every developer uses daily:

In simple words: `isBlank`, `strip`, `repeat` and `lines` are one-line helpers that replace loops you used to write by hand — for example, "AB".repeat(3) gives "ABABAB" in a single call.
Example02
JCode Cell
1import java.util.*;
2 
3class StringDemo11 {
4 public static void main(String[] args) {
5 // isBlank() - is the string empty or only spaces?
6 System.out.println(" ".isBlank()); // true
7 System.out.println("Hi".isBlank()); // false
8 
9 // strip() - remove edge spaces (better than trim())
10 System.out.println(" Java ".strip()); // Java
11 
12 // repeat(n) - repeat the string n times
13 System.out.println("AB".repeat(3)); // ABABAB
14 
15 // lines() - split into lines as a stream
16 "one\ntwo\nthree".lines().forEach(System.out::println);
17 }
18}
Output
true false Java ABABAB one two three

strip() vs trim() — Small but Important

Pointtrim() (old)strip() (Java 11)
Introduced inJava 1.0Java 11
RemovesSpaces with code <= 32 (ASCII spaces)All Unicode spaces (including tab, newline)
StandardOld, ASCII-basedModern, Unicode-aware
Best choiceLegacy codeNew code
In simple words: `strip()` removes all Unicode spaces, while `trim()` only removes ASCII spaces — for new code always prefer strip(), because it also clears tabs, newlines and other Unicode whitespace.

var in Lambda Parameters

Java 11 allows var inside lambda parameters — useful when you want to add annotations to the parameter:

Trainer's Note: The real use of var in lambdas is when you need annotations on the parameter, like (@NotNull var s) -> .... Otherwise, plain s is shorter. So this feature is more about consistency than daily use.
Example04
JCode Cell
1// Java 10: (var not allowed here)
2list.forEach((s) -> System.out.println(s));
3 
4// Java 11: var allowed
5list.forEach((var s) -> System.out.println(s));

Run a Single Java File Directly

Before Java 11, running a program needed two steps: javac Hello.java then java Hello. Java 11 added a shortcut — the single-file source-code launch. It compiles in memory and runs:

This works only for a single file program (no external dependencies). Great for quick tests and small scripts — and it made learning Java simpler.

In simple words: `java Hello.java` compiles the file in memory and runs it in one command — no javac step and no .class file left behind, which is perfect for quick tests and small scripts.
Example05
JCode Cell
1C:\> java Hello.java // compiles and runs in one command

The New HttpClient

Java 11 made the HttpClient standard (it was an incubator in Java 9). It lets Java programs call web APIs cleanly — a huge topic for modern apps:

Example06
JCode Cell
1import java.net.URI;
2import java.net.http.*;
3 
4class HttpDemo {
5 public static void main(String[] args) throws Exception {
6 HttpClient client = HttpClient.newHttpClient();
7 
8 HttpRequest request = HttpRequest.newBuilder()
9 .uri(URI.create("https://www.example.com"))
10 .GET()
11 .build();
12 
13 HttpResponse<String> response = client.send(request,
14 HttpResponse.BodyHandlers.ofString());
15 
16 System.out.println("Status: " + response.statusCode());
17 System.out.println("Body (first line): " +
18 response.body().lines().findFirst().orElse(""));
19 }
20}
Output
Status: 200 Body (first line): <!doctype html>

Other Java 11 Changes

  • Removed old Java EE modules (CORBA, JAXB, JAX-WS) — the platform became lighter.
  • Removed finalize() from use (deprecated; see Garbage Collection chapter).
  • Nest-based access control — a technical speed-up for nested classes.
  • ZGC and Epsilon — experimental new garbage collectors (ZGC became stable in Java 15).
  • TLS 1.3 support — modern secure connections.

Java 11 is still widely used in industry today. Companies that moved from Java 8 usually went to 11 first, then to 17 and 21.

📝 Key Takeaways
  • Java 11 is an LTS version — trusted by companies for long-term support.
  • New String methods: isBlank(), strip(), repeat(), lines().
  • var is allowed in lambda parameters (mainly for annotations).
  • java Hello.java compiles and runs a single file in one command.
  • HttpClient is now standard for calling web APIs.
  • Old Java EE modules were removed to make the platform lighter.

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8