If using JDK 9 +, use Objects.requireNonNullElse(T obj, T defaultObj)

Answer from AK4647 on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › convert null value to a default value in java
Convert Null Value to a Default Value in Java | Baeldung
June 20, 2024 - It mirrors the Java functionality and doesn’t add any explicitly useful features. To get a default value if the provided is null, we can use MoreObjects:
🌐
HowDev
how.dev › answers › what-is-objectutilsdefaultifnull-in-java
What is ObjectUtils.defaultIfNull in Java?
describes the methods in Java that can be called without creating an object of the class of ObjectUtils that returns the default value if the passed object is null.
🌐
Better Programming
betterprogramming.pub › checking-for-nulls-in-java-minimize-using-if-else-edae27016474
Checking for Nulls in Java? Minimize Using “If Else” | by Itır ...
January 26, 2022 - If you are not familiar with Lombok, I highly suggest you to check it out. I personally love Lombok and it makes a developer’s life much easier :) Lets say that you have Student.javawith fields such as id, name and classes. You can use put @Builder.Default before the related field and give it a default value. When an instance of this Student class is created, it will have “classes” as an empty list, not null...
🌐
Quora
quora.com › What-is-the-difference-between-a-null-value-and-a-default-value-in-Java-Can-a-pointer-have-a-null-value-in-Java-If-so-how-would-you-achieve-this
What is the difference between a null value and a default value in Java? Can a pointer have a null value in Java? If so, how would you achieve this? - Quora
Answer (1 of 2): What is the difference between a null value and a default value in Java? Let’s start with the basics - all non-primitive values are Objects in java, i.e. a reference or pointer to a memory location containing formatted data corresponding to a class definition. For those not fam...
🌐
GeeksforGeeks
geeksforgeeks.org › java › replace-null-values-with-default-value-in-java-map
Replace null values with default value in Java Map - GeeksforGeeks
July 11, 2025 - // Java program to replace null values // of a map with a default value import java.util.*; import java.util.stream.*; class GFG { // Function to replace the null values public static <T, K> Map<K, T> replaceNullValues(Map<K, T> map, T defaultValue) { // Replace the null value map = map.entrySet() .stream() .map(entry -> { if (entry.getValue() == null) entry.setValue(defaultValue); return entry; }) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); return map; } public static void main(String[] args) { // Get the map Map<Integer, Integer> map = new HashMap<>(); map.put(1, 1);
🌐
Educative
educative.io › answers › what-is-stringutilsdefaultifempty-in-java
What is StringUtils.defaultIfEmpty in Java?
The method takes in two parameters, i.e., a string and a default string. If the passed string is null or empty then the default string is returned.
🌐
Coderanch
coderanch.com › t › 549792 › certification › Default-String-null
Default value of String: null or (OCPJP forum at Coderanch)
Your String reference "a" is not referring to anything here, hence it is null (following the default behavior of Object references of Java). And the default value of an String object (unless you give it a value while instantiating) is the "empty" String. Hence the behavior you observed is correct.
Find elsewhere
🌐
Foojay
foojay.io › home › optional in java: a swiss army knife for handling nulls and improving code quality
Optional in Java: A Swiss Army Knife for Handling Nulls and Improving Code Quality
February 20, 2023 - In this example, we create a list of Optional values, use Optional.stream() to create a stream of non-empty values, and then use stream methods to join the non-empty values into a single string. This allows us to handle collections of Optional values more elegantly and expressively. Optional can simplify configuration management by providing a default value for a configuration parameter. For example, if you have a configuration parameter that might be missing, you can use Optional to provide a default value if the parameter is not present.
🌐
GitHub
github.com › FasterXML › jackson-future-ideas › issues › 39
set default value if null (or meet some condition) in deserialization · Issue #39 · FasterXML/jackson-future-ideas
May 21, 2019 - I'm wondering if we can have an annotation (field level and class level) to set a default value if the corresponding JSON value is null in deserialization. { "name": "Jack" } class People { String name; @JsonDefault(intDefault = 10) Inte...
Published   May 21, 2019
🌐
Oracle
oracle.com › java › technical details
Tired of Null Pointer Exceptions? Consider Using Java SE 8's Optional!
Similarly, you can use the ... a default value if Optional is empty, throws an exception: ... Often you need to call a method on an object and check some property. For example, you might need to check whether the USB port is a particular version. To do this in a safe way, you first need to check whether the reference pointing to a USB object is null and then call the ...
🌐
Baeldung
baeldung.com › home › java › avoid check for null statement in java
Avoid Check for Null Statement in Java | Baeldung
January 8, 2024 - Learn several strategies for avoiding the all-too-familiar boilerplate conditional statements to check for null values in Java.
🌐
Upwork
upwork.com › resources › articles › {name}
Null in Java: Understanding the Basics - Upwork
August 5, 2024 - Java programmers usually encounter this infamous pointer exception when they forget to initialize a variable (because null is the default value for uninitialized reference variables).
🌐
Medium
medium.com › thefreshwrites › java-optional-and-either-handling-null-values-and-representing-two-possible-values-6a477a0fe189
Java Optional and Either: Handling Null Values and Representing Two Possible Values | by Samuel Catalano | Mar, 2023 | Medium | The Fresh Writes
March 10, 2023 - In this example, optionalName is an Optional that contains a value if name is not null, otherwise it is empty. The orElse() method returns the value if it is present, otherwise it returns the default value “Unknown”. Java Either is a class that provides a way to represent one of two possible values.
🌐
Code2care
code2care.org › home page › java › set default value optional empty or null in java
How to set a Default Value for Optional is Empty or Null in Java | Code2care
October 9, 2023 - import java.util.Optional; public class Example { public static void main(String[] args) { Optional<String> optionalWithValue = Optional.of("Mike"); Optional<String> optionalEmpty = Optional.empty(); // empty Optional<String> optionalNull = Optional.ofNullable(null); // null String value1 = optionalWithValue.orElse("No Name"); String value2 = optionalEmpty.orElse("Default Value"); String value3 = optionalNull.orElse("0"); System.out.println("Value 1: " + value1); // Output: Value 1: Mike System.out.println("Value 2: " + value2); // Output: Value 2: Default Value System.out.println("Value 3: " + value3); // Output: Value 3: 0 } } ... public T orElse(T other) If a value is present, return the value, otherwise, return the other.