working with null pointers in Java 8 java 7 Objects require non null or else Optional

Published: 26 May 2024
on channel: CodeWis Technologies by Nuhman Paramban
27
0

working with null pointers in Java 8 java 7 Objects require non null or else Optional

The provided Java code defines a utility class AppUtils with methods to handle and test for null values in strings. Below is a detailed explanation of each part of the code, including the commented-out lines.

Package Declaration
java
Copy code
package com.nuhman.coding.test;
This declares the package com.nuhman.coding.test which helps in organizing classes into namespaces.

Imports
java
Copy code
import java.time.LocalDate;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
These import statements include:

LocalDate for handling date values.
Objects for utility methods related to object operations.
Optional for handling potentially null values more gracefully.
Supplier for providing supplier functional interface.
Class Declaration
java
Copy code
public class AppUtils {
Defines the AppUtils class as public, meaning it can be accessed from other classes.

Method testStringNulls
java
Copy code
public static void testStringNulls (String str) {
A static method testStringNulls that takes a String parameter str.

Commented-Out Code
The following commented-out lines provide various ways to handle null values in strings:

Direct Assignment

java
Copy code
//String str2 = str;
Simply assigns str to str2.

Using Objects.requireNonNull

java
Copy code
//String str2 = Objects.requireNonNull(str);
Throws NullPointerException if str is null.

java
Copy code
//String str2 = Objects.requireNonNull(str, "So you need to supply non null values");
Throws NullPointerException with a custom message if str is null.

Using Objects.requireNonNullElse

java
Copy code
//String str2 = Objects.requireNonNullElse(str, "Hi, I am a Default string");
Returns str if it is non-null; otherwise, returns the default string "Hi, I am a Default string".

Using Supplier and Objects.requireNonNullElseGet

java
Copy code
Supplier < String > strSupplier = () - > LocalDate.now().toString();
//String str2 = Objects.requireNonNullElseGet(str, strSupplier);
Returns str if it is non-null; otherwise, returns the result of the strSupplier which supplies the current date as a string.

java
Copy code
//String str2 = Objects.requireNonNullElseGet(str, ()-> LocalDate.now().toString());
Similar to the previous line but uses a lambda expression directly instead of a named supplier.

Active Code
java
Copy code
String str2 = Optional.ofNullable(str).orElse("nonNull");
System.out.println(str2);
Optional.ofNullable(str) creates an Optional object from str, which may be null.
.orElse("nonNull") returns the value of str if it is non-null; otherwise, it returns the string "nonNull".
System.out.println(str2); prints the resulting string.
main Method
java
Copy code
public static void main(String[] args) {
testStringNulls(null);
}
The main method is the entry point of the program.
It calls the testStringNulls method with null as the argument, which demonstrates how the method handles null values.
Execution Flow
When main is executed:

testStringNulls(null) is called.
Inside testStringNulls, Optional.ofNullable(null).orElse("nonNull") evaluates to "nonNull".
"nonNull" is printed to the console.
Overall, this code demonstrates various ways to handle null strings in Java, especially using the Objects utility class and Optional for more robust and readable null-checking mechanisms.


Watch video working with null pointers in Java 8 java 7 Objects require non null or else Optional online, duration hours minute second in high quality that is uploaded to the channel CodeWis Technologies by Nuhman Paramban 26 May 2024. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 27 times and liked it 0 visitors.