This Java code demonstrates how Java 9 introduced new utility methods in the Objects
class for handling null values, index checks, and working with collections. The class java9Additionals
contains a method objectsClassModificationsInJava9
, which showcases these new methods. Below is a detailed breakdown of the code, including explanations for each method and the commented-out lines.
1. Package and Imports
java
package com.nuhman.coding.test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
- The
package
statement defines that the class is part of the com.nuhman.coding.test
package. Arrays
and Collections
are imported for working with collections.Objects
provides utility methods for object-related operations, particularly for null checks and handling common tasks involving collections and indices.
2. Class Declaration
java
public class java9Additionals {
- The
java9Additionals
class is declared as public
, meaning it is accessible from other packages.
3. Method objectsClassModificationsInJava9
java
public static void objectsClassModificationsInJava9(List<String> stringList) {
- This static method is declared with a single parameter,
stringList
, which is a List
of String
objects. It demonstrates the usage of new methods from the Objects
class.
4. Commented-Out Code: New Methods in Java 9
Using requireNonNullElse
java
// 1 . requireNonNullElse method
List<String> strList = Objects.requireNonNullElse(stringList, Collections.EMPTY_LIST);
System.out.println(strList);
Objects.requireNonNullElse(stringList, Collections.EMPTY_LIST)
checks if stringList
is null
. If it is, it returns an empty list (Collections.EMPTY_LIST
); otherwise, it returns the provided list (stringList
).- This is a cleaner and more concise way of handling
null
values compared to manually checking if (stringList == null)
.
Using requireNonNullElseGet
java
// 2 . requireNonNullElseGet method
strList = Objects.requireNonNullElseGet(stringList, List::of);
System.out.println(strList);
Objects.requireNonNullElseGet(stringList, List::of)
works similarly to requireNonNullElse
, but if stringList
is null
, it uses the provided Supplier
(List::of
) to generate a new empty immutable list.- In this case,
List::of
is a method reference that provides an empty list if stringList
is null
.
Using checkIndex
java
// 3 . checkIndex method
int checkIndex = Objects.checkIndex(strList.size(), 1);
System.out.println(checkIndex);
// replaces if(strList.size()==1) throws exception
Objects.checkIndex(strList.size(), 1)
checks if the index 1
is within bounds of the list strList
(i.e., if the list has at least two elements).- If the index is out of bounds, it throws an
IndexOutOfBoundsException
. This method replaces manual index validation logic like if (strList.size() == 1)
.
Using checkFromToIndex
java
// 4 . checkFromToIndex method
int startIndex = 0;
int endIndex = 1;
checkIndex = Objects.checkFromToIndex(startIndex, endIndex, 1);
System.out.println(checkIndex);
Objects.checkFromToIndex(startIndex, endIndex, 1)
checks if the range from startIndex
to endIndex
is within bounds of a list of size 1
.- It ensures that the range does not exceed the list bounds. If the range is invalid, it throws
IndexOutOfBoundsException
.
Using checkFromIndexSize
java
// 5 . checkFromIndexSize method
startIndex = 2;
endIndex = 4;
checkIndex = Objects.checkFromIndexSize(startIndex, endIndex, 6);
System.out.println(checkIndex);
Objects.checkFromIndexSize(startIndex, endIndex, 6)
checks if a sublist, starting from startIndex
and with a size of endIndex - startIndex
, fits within a list of size 6
.- It ensures that the sublist can be accessed without throwing an exception. If the indices are invalid, it throws
IndexOutOfBoundsException
.
5. Main Method
java
public static void main(String[] args) {
objectsClassModificationsInJava9(null);
objectsClassModificationsInJava9(Arrays.asList("str", "str2"));
}
- The
main
method demonstrates calling objectsClassModificationsInJava9
with two different arguments: - First call:
null
is passed, which tests how the methods handle null values. - Second call: A
List
containing two strings: "str"
and "str2"
. This demonstrates handling non-null lists and performing various index checks.
Execution Flow
- When
objectsClassModificationsInJava9(null)
is called:
requireNonNullElse
returns an empty list (Collections.EMPTY_LIST
).requireNonNullElseGet
returns an empty immutable list (List::of
).checkIndex
will throw an exception as index 1 is out of bounds for an empty list.checkFromToIndex
and checkFromIndexSize
will throw exceptions due to invalid ranges.
- When
objectsClassModificationsInJava9(Arrays.asList("str", "str2"))
is called:
requireNonNullElse
returns the list ["str", "str2"]
.requireNonNullElseGet
returns the list ["str", "str2"]
.checkIndex
validates index 1 successfully since the list contains two elements.checkFromToIndex
validates the range from index 0 to index 1, which is within bounds.checkFromIndexSize
ensures that the sublist range from index 2 to index 4 is valid, throwing an exception as the sublist is out of bounds.
Conclusion
This code demonstrates the new Objects
methods introduced in Java 9, which simplify common operations like null-checking and index validation:
requireNonNullElse
and requireNonNullElseGet
offer cleaner ways to handle null
values by providing default or generated fallback values.checkIndex
, checkFromToIndex
, and checkFromIndexSize
provide more reliable and concise methods for validating indices and ranges, reducing the need for manual checks and making the code more robust and readable.
These improvements in Java 9 make it easier to write cleaner, more efficient code when dealing with collections and potential null
values.