Java 9
1. Factory Methods for Collections
List
, Set
, and Map
.Key Points:
- Provides static methods for initializing immutable collections.
- No modifications (add, remove, update) allowed after initialization.
- Simplifies code compared to traditional
Arrays.asList()
orCollections.unmodifiableList()
.
Examples:
2. Private Interface Methods
Java 9 allows private methods inside interfaces to eliminate redundant code in default and static methods.
Key Points:
- Enhances code reusability inside interfaces.
- Private methods cannot be accessed outside the interface.
Example:
Java 10
Local Variable Type Inference (var
)
Java 10 introduces var
for type inference in local variables, reducing boilerplate code.
Key Points:
var
can only be used for local variables inside methods, loops, or initialization blocks.- Cannot be used for class fields, method parameters, or return types.
- In JDK 10 and later, you can declare local variables with non-null initializers with the var identifier, which can help you write code that’s easier to read.
- Note: that this feature is available only for local variables with the initializer.
Example:
Valid Uses of var
:
class A {public static void main(String a[]){
var list = new ArrayList<String>(); // infers ArrayList<String>
var stream = list.stream(); // infers Stream<String> // Declaration of a local variable in java 10 using LVTI
var x = "Hi there"; // Declaring index variables in for loops using LVTI in Java
for (var x = 0; x < 3; x++) {
System.out.println(x);
}
System.out.println(x)
}
}
class A {public static void main(String a[]){
var list = new ArrayList<String>(); // infers ArrayList<String>
var stream = list.stream(); // infers Stream<String> // Declaration of a local variable in java 10 using LVTI
var x = "Hi there"; // Declaring index variables in for loops using LVTI in Java
for (var x = 0; x < 3; x++) {
System.out.println(x);
}
System.out.println(x)
}
}
Invalid Uses of var
:
class A {**//Not permitted in class fields**(non-local variables**)
**public var = "hello"; // error: 'var' is not allowed here**//Not allowed as parameter for any methods**
void show(var a) /*Error:
{
}**//Not permitted in method return type
**public var show() /* Error: {
return 1;
}
}
Java 11
New Utility Methods in String
Java 11 adds several new methods to the String
class to improve usability.
isBlank()
: Checks if a string is empty or contains only white spaces.
lines()
: Splits a string into a stream of lines.
repeat(n)
: Repeats a stringn
times.
- strip(): It is used to remove the white-spaces which are in-front and back of the string.strip() is “Unicode-aware” evolution of trim(). When trim() was introduced, Unicode wasn’t evolved. Now, the new strip() removes all kinds of whitespaces leading and trailing(check the method Character.isWhitespace(c) to know if a unicode is whitespace or not)
- stripTrailing(): It is used to remove the white-space which is in back of the string
- stripLeading(): It is used to remove the white-space which is in-front of the string
Example:
String str = "1".repeat(5);
String str = "Hi\nHello\nNamaste";
System.out.println(str.lines().collect(Collectors.toList()));
String str = "1".repeat(5);
String str = "Hi\nHello\nNamaste";
System.out.println(str.lines().collect(Collectors.toList()));
public class HelloWorld {
public static void main(String[] args)
{
System.out.println(" hi ".strip());
System.out.println(" hi ".stripLeading());
System.out.println(" hi ".stripTrailing());
}
}
Output:
"hi"
"hi "
" hi"
public class HelloWorld {
public static void main(String[] args)
{
System.out.println(" hi ".strip());
System.out.println(" hi ".stripLeading());
System.out.println(" hi ".stripTrailing());
}
}
Output:
"hi"
"hi "
" hi"
New File Methods
Java 11 introduces Files.readString()
and Files.writeString()
to simplify file handling.
Example:
New Not Predicate Method
Example:
Java 12
Enhanced switch
Expressions
Java 12 introduces an improved switch
statement that allows using expressions.
Key Points:
- Removes the need for
break
. - Can return values directly.
Example:
Java 14
1. Records
Records provide a concise way to declare immutable data classes, automatically generating toString()
, equals()
, and hashCode()
.
A record is a data class that stores pure data. The idea behind introducing records is to quickly create simple and concise classes devoid of boilerplate code in the model POJOs.
The important difference between class and record is that a record aims to eliminate all the boilerplate code needed to set and get the data from instance. Records transfer this responsibility to java compiler which generates the constructor, field getters, hashCode() and equals() as well toString() methods.
Example:
2. Helpful NullPointerExceptions
Java 14 improves error messages when NullPointerException
occurs.
Example:
3. Pattern Matching for instanceof
Simplifies instanceof
by eliminating redundant casting.
Example:
Java 15
Text Blocks
Multi-line string literals improve readability and reduce the need for escape sequences.
Text blocks are finally a standard feature in Java 15. A text block is a multi-line string literal.
While using text blocks, we do not need to provide explicit line terminators, string concatenations, and delimiters otherwise used for writing the normal string literals.
Text blocks start with a “”” (three double-quote marks) followed by optional whitespaces and a newline
Example:
Java 16
Stream.toList()
Method
Java 16 introduces Stream.toList()
to simplify stream collection operations.
Example:
Java 17
Sealed Classes
Sealed classes restrict which classes can extend them, providing better control over class hierarchies.
Note: Only final,sealed or non-sealed class can extend sealclass
Example:
These Java versions introduce powerful features that simplify development, improve performance, and enhance code readability.
Post a Comment