Java 11 brought additional enhancements and features, such as the new HTTP client, local-variable syntax for lambda parameters, and various performance improvements. Staying updated with these features is crucial for modern Java development.
To learn more about Java 11 features, check out our article: Java 11 Features.
Java 11 introduced several new features, including the var
keyword for local variable type inference.
var list = new ArrayList<String>(); // Type inferred as ArrayList
HttpClient
API?The HttpClient
API provides a modern way to send HTTP requests and handle responses.
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String::repeat
method?The String::repeat
method allows you to repeat a string a specified number of times.
String repeated = "abc".repeat(3); // "abcabcabc"
Files::readString
method?The Files::readString
method reads the content of a file as a string.
String content = Files.readString(Path.of("file.txt"));
Optional::isEmpty
method?The Optional::isEmpty
method checks if an Optional
is empty.
Optional<String> optional = Optional.empty();
boolean isEmpty = optional.isEmpty(); // true
var
keyword used for?The var
keyword is used for local variable type inference, allowing the compiler to infer the type.
HttpRequest
class?The HttpRequest
class represents an HTTP request, including its method, URI, headers, and body.
HttpResponse
class?The HttpResponse
class represents an HTTP response, including its status code, headers, and body.
HttpClient.newBuilder()
method?The HttpClient.newBuilder()
method creates a new instance of the HttpClient.Builder
class.
HttpResponse.BodyHandlers
class?The HttpResponse.BodyHandlers
class provides various body handlers for processing the response body.
String::strip
method?The String::strip
method removes leading and trailing whitespace from a string.
String str = " Hello ".strip(); // "Hello"
String::lines
method?The String::lines
method returns a stream of lines extracted from a string.
String multiline = "Line 1\nLine 2";
multiline.lines().forEach(System.out::println);
Files::writeString
method?The Files::writeString
method writes a string to a file.
Files.writeString(Path.of("file.txt"), "Hello, World!");
Files::writeString
method's second parameter?The second parameter of Files::writeString
specifies the options for writing, such as StandardOpenOption.CREATE
.
HttpClient::sendAsync
method?The HttpClient::sendAsync
method sends an HTTP request asynchronously and returns a CompletableFuture
.
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenAccept(response -> System.out.println(response.body()));
CompletableFuture
class?The CompletableFuture
class represents a future result of an asynchronous computation.
String::toArray
method?The String::toArray
method converts a stream of strings into an array.
String[] array = list.stream().toArray(String[]::new);
Optional::orElseThrow
method?The Optional::orElseThrow
method returns the value if present, otherwise throws an exception.
String value = optional.orElseThrow(() -> new IllegalArgumentException("Value not present"));
String::formatted
method?The String::formatted
method returns a formatted string using the specified format string and arguments.
String formatted = "Hello, %s".formatted("World"); // "Hello, World"
var
keyword's limitation?The var
keyword can only be used for local variables and cannot be used for method parameters or return types.
HttpClient::version
method?The HttpClient::version
method returns the version of the HTTP protocol used by the client.
HttpRequest.Builder
class?The HttpRequest.Builder
class is used to construct an HttpRequest
instance.
HttpResponse.BodyPublishers
class?The HttpResponse.BodyPublishers
class provides body publishers for sending request bodies.
HttpResponse.BodyHandlers.ofString()
method?The HttpResponse.BodyHandlers.ofString()
method returns a body handler that converts the response body to a string.
String::toUpperCase
method?The String::toUpperCase
method converts all characters in a string to uppercase.
String upper = "hello".toUpperCase(); // "HELLO"
String::toLowerCase
method?The String::toLowerCase
method converts all characters in a string to lowercase.
String lower = "HELLO".toLowerCase(); // "hello"
String::substring
method?The String::substring
method returns a new string that is a substring of the original string.
String sub = "Hello".substring(1); // "ello"
String::indexOf
method?The String::indexOf
method returns the index of the first occurrence of a specified character or substring.
int index = "Hello".indexOf('e'); // 1
String::contains
method?The String::contains
method checks if a string contains a specified sequence of characters.
boolean contains = "Hello".contains("ell"); // true
String::split
method?The String::split
method splits a string into an array based on a specified delimiter.
String[] parts = "a,b,c".split(","); // ["a", "b", "c"]
String::replace
method?The String::replace
method replaces all occurrences of a specified character or substring with another.
String replaced = "Hello".replace("e", "a"); // "Hallo"
String::trim
method?The String::trim
method removes leading and trailing whitespace from a string.
String trimmed = " Hello ".trim(); // "Hello"
String::valueOf
method?The String::valueOf
method converts different types of values to their string representation.
String str = String.valueOf(123); // "123"
String::join
method?The String::join
method joins multiple strings with a specified delimiter.
String joined = String.join(",", "a", "b", "c"); // "a,b,c"
String::compareTo
method?The String::compareTo
method compares two strings lexicographically.
int result = "abc".compareTo("abd"); // negative value
String::equalsIgnoreCase
method?The String::equalsIgnoreCase
method compares two strings, ignoring case considerations.
boolean isEqual = "abc".equalsIgnoreCase("ABC"); // true
String::startsWith
method?The String::startsWith
method checks if a string starts with a specified prefix.
boolean starts = "Hello".startsWith("He"); // true
String::endsWith
method?The String::endsWith
method checks if a string ends with a specified suffix.
boolean ends = "Hello".endsWith("lo"); // true
String::charAt
method?The String::charAt
method returns the character at a specified index.
char ch = "Hello".charAt(1); // 'e'
String::lastIndexOf
method?The String::lastIndexOf
method returns the index of the last occurrence of a specified character or substring.
int lastIndex = "Hello".lastIndexOf('l'); // 3
String::replaceAll
method?The String::replaceAll
method replaces each substring of a string that matches a given regular expression with a given replacement.
String replacedAll = "abc123".replaceAll("\\d", "#"); // "abc###"
String::matches
method?The String::matches
method checks if a string matches a given regular expression.
boolean matches = "abc".matches("[a-z]+"); // true
String::split
method's limitation?The String::split
method can produce empty strings in the resulting array if the string ends with the delimiter.
String::toCharArray
method?The String::toCharArray
method converts a string into a character array.
char[] chars = "Hello".toCharArray(); // ['H', 'e', 'l', 'l', 'o']
String::codePoints
method?The String::codePoints
method returns an IntStream of Unicode code points from the string.
IntStream codePoints = "Hello".codePoints();
String::formatted
method's advantage?The String::formatted
method provides a convenient way to format strings using format specifiers.
String::compareToIgnoreCase
method?The String::compareToIgnoreCase
method compares two strings lexicographically, ignoring case differences.
int result = "abc".compareToIgnoreCase("ABC"); // 0
String::concat
method?The String::concat
method concatenates the specified string to the end of the current string.
String concatenated = "Hello".concat(" World"); // "Hello World"
String::getBytes
method?The String::getBytes
method encodes the string into a sequence of bytes using the platform's default charset.
byte[] bytes = "Hello".getBytes();
String::intern
method?The String::intern
method returns a canonical representation for the string object.
String interned = "Hello".intern();