Java 11 Features

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.

Questions and Answers on Java 11 Features

1. What is a new feature introduced in Java 11?

Java 11 introduced several new features, including the var keyword for local variable type inference.

var list = new ArrayList<String>(); // Type inferred as ArrayList

2. What is the 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());

3. What is the String::repeat method?

The String::repeat method allows you to repeat a string a specified number of times.

String repeated = "abc".repeat(3); // "abcabcabc"

4. What is the Files::readString method?

The Files::readString method reads the content of a file as a string.

String content = Files.readString(Path.of("file.txt"));

5. What is the Optional::isEmpty method?

The Optional::isEmpty method checks if an Optional is empty.

Optional<String> optional = Optional.empty();
boolean isEmpty = optional.isEmpty(); // true

6. What is the var keyword used for?

The var keyword is used for local variable type inference, allowing the compiler to infer the type.

7. What is the HttpRequest class?

The HttpRequest class represents an HTTP request, including its method, URI, headers, and body.

8. What is the HttpResponse class?

The HttpResponse class represents an HTTP response, including its status code, headers, and body.

9. What is the HttpClient.newBuilder() method?

The HttpClient.newBuilder() method creates a new instance of the HttpClient.Builder class.

10. What is the HttpResponse.BodyHandlers class?

The HttpResponse.BodyHandlers class provides various body handlers for processing the response body.

11. What is the String::strip method?

The String::strip method removes leading and trailing whitespace from a string.

String str = "   Hello   ".strip(); // "Hello"

12. What is the 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);

13. What is the Files::writeString method?

The Files::writeString method writes a string to a file.

Files.writeString(Path.of("file.txt"), "Hello, World!");

14. What is the Files::writeString method's second parameter?

The second parameter of Files::writeString specifies the options for writing, such as StandardOpenOption.CREATE.

15. What is the 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()));

16. What is the CompletableFuture class?

The CompletableFuture class represents a future result of an asynchronous computation.

17. What is the String::toArray method?

The String::toArray method converts a stream of strings into an array.

String[] array = list.stream().toArray(String[]::new);

18. What is the 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"));

19. What is the 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"

20. What is the var keyword's limitation?

The var keyword can only be used for local variables and cannot be used for method parameters or return types.

21. What is the HttpClient::version method?

The HttpClient::version method returns the version of the HTTP protocol used by the client.

22. What is the HttpRequest.Builder class?

The HttpRequest.Builder class is used to construct an HttpRequest instance.

23. What is the HttpResponse.BodyPublishers class?

The HttpResponse.BodyPublishers class provides body publishers for sending request bodies.

24. What is the HttpResponse.BodyHandlers.ofString() method?

The HttpResponse.BodyHandlers.ofString() method returns a body handler that converts the response body to a string.

25. What is the String::toUpperCase method?

The String::toUpperCase method converts all characters in a string to uppercase.

String upper = "hello".toUpperCase(); // "HELLO"

26. What is the String::toLowerCase method?

The String::toLowerCase method converts all characters in a string to lowercase.

String lower = "HELLO".toLowerCase(); // "hello"

27. What is the 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"

28. What is the 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

29. What is the String::contains method?

The String::contains method checks if a string contains a specified sequence of characters.

boolean contains = "Hello".contains("ell"); // true

30. What is the 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"]

31. What is the 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"

32. What is the String::trim method?

The String::trim method removes leading and trailing whitespace from a string.

String trimmed = "   Hello   ".trim(); // "Hello"

33. What is the String::valueOf method?

The String::valueOf method converts different types of values to their string representation.

String str = String.valueOf(123); // "123"

34. What is the String::join method?

The String::join method joins multiple strings with a specified delimiter.

String joined = String.join(",", "a", "b", "c"); // "a,b,c"

35. What is the String::compareTo method?

The String::compareTo method compares two strings lexicographically.

int result = "abc".compareTo("abd"); // negative value

36. What is the String::equalsIgnoreCase method?

The String::equalsIgnoreCase method compares two strings, ignoring case considerations.

boolean isEqual = "abc".equalsIgnoreCase("ABC"); // true

37. What is the String::startsWith method?

The String::startsWith method checks if a string starts with a specified prefix.

boolean starts = "Hello".startsWith("He"); // true

38. What is the String::endsWith method?

The String::endsWith method checks if a string ends with a specified suffix.

boolean ends = "Hello".endsWith("lo"); // true

39. What is the String::charAt method?

The String::charAt method returns the character at a specified index.

char ch = "Hello".charAt(1); // 'e'

40. What is the 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

41. What is the 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###"

42. What is the String::matches method?

The String::matches method checks if a string matches a given regular expression.

boolean matches = "abc".matches("[a-z]+"); // true

43. What is the String::split method's limitation?

The String::split method can produce empty strings in the resulting array if the string ends with the delimiter.

44. What is the String::toCharArray method?

The String::toCharArray method converts a string into a character array.

char[] chars = "Hello".toCharArray(); // ['H', 'e', 'l', 'l', 'o']

45. What is the String::codePoints method?

The String::codePoints method returns an IntStream of Unicode code points from the string.

IntStream codePoints = "Hello".codePoints();

46. What is the String::formatted method's advantage?

The String::formatted method provides a convenient way to format strings using format specifiers.

47. What is the String::compareToIgnoreCase method?

The String::compareToIgnoreCase method compares two strings lexicographically, ignoring case differences.

int result = "abc".compareToIgnoreCase("ABC"); // 0

48. What is the 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"

49. What is the 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();

50. What is the String::intern method?

The String::intern method returns a canonical representation for the string object.

String interned = "Hello".intern();
Go Back Home!!
Subscribe to Our Newsletter

Get the latest updates and exclusive content delivered to your inbox!