You can't do it directly, you should provide your own way to check this. Eg.

class MyClass {
  Object attr1, attr2, attr3;

  public boolean isValid() {
    return attr1 != null && attr2 != null && attr3 != null;
  }
}

Or make all fields final and initialize them in constructors so that you can be sure that everything is initialized.

Answer from Jack on Stack Overflow
🌐
Java Guides
javaguides.net › 2024 › 05 › java-check-if-object-is-null-or-empty.html
Java: Check If Object Is Null or Empty
May 29, 2024 - This guide will cover various ways to check if different types of objects (like String, Collection, Map, and custom objects) are null or empty.
🌐
Baeldung
baeldung.com › home › java › avoid check for null statement in java
Avoid Check for Null Statement in Java | Baeldung
April 8, 2019 - This presents the need for us to check the parameters or the response for a null value. Here, we can use Java Assertions instead of the traditional null check conditional statement: public void accept(Object param){ assert param != null; ...
🌐
Medium
tamerardal.medium.com › simplify-null-checks-in-java-writing-clean-code-with-apache-commons-lang-3-e7d3aea207bd
Simplify Null Checks in Java: Writing Clean Code with Apache Commons Lang 3 | by Tamer Ardal | Medium
September 18, 2024 - Simplify Null Checks in Java: Writing Clean Code with Apache Commons Lang 3 In Java, null checks are generally done using == or !=. In addition, if we want to do an empty check, our condition will …
🌐
Baeldung
baeldung.com › home › java › core java › check if all the variables of an object are null
Check If All the Variables of an Object Are Null | Baeldung
January 8, 2024 - To exemplify, we’ll use the Car class defined below: ... The simplest way is using a sequence of if statements to check field by field if they are null or not and, in the end, return a combination of their results.
🌐
TutorialsPoint
tutorialspoint.com › checking-for-null-or-empty-in-java
Checking for Null or Empty in Java.
We can check whether a particular String is empty or not, using the isBlank() method of the StringUtils class. This method accepts an integer as a parameter and returns true if the given string is empty, or false if it is not. The following is an example of checking if a string is null or empty ...
🌐
LabEx
labex.io › tutorials › java-how-to-check-if-an-object-is-null-in-java-560011
How to Check If an Object Is Null in Java | LabEx
Learn how to check if an object is null in Java using the equality operator, combining null and type checks, and the Optional class to prevent NullPointerException errors and write more robust code.
🌐
Stack Abuse
stackabuse.com › java-check-if-string-is-null-empty-or-blank
Java: Check if String is Null, Empty or Blank
February 28, 2023 - String nullString = null; String emptyString = ""; String blankString = " "; In this tutorial, we'll look at how to check if a String is Null, Empty or Blank in Java. As mentioned before, a string is empty if its length is equal to zero. We will be using the length() method, which returns the ...
🌐
Educative
educative.io › answers › what-is-objectutilsisempty-in-java
What is ObjectUtils.isEmpty in Java?
System.out.println("The output of ObjectUtils.isEmpty() when a non-empty list is passed is " + ObjectUtils.isEmpty(stringList));
Find elsewhere
🌐
Programiz
programiz.com › java-programming › examples › string-empty-null
Java Program to Check if a String is Empty or Null
To understand this example, you ... · Java String isEmpty() Java String trim() class Main { public static void main(String[] args) { // create null, empty, and regular strings String str1 = null; String str2 = ""; String str3 = " "; // check if str1 is null or empty System.out.println("str1 is " + isNullEmpty(str1)); // check if ...
🌐
Delft Stack
delftstack.com › home › howto › java › java check if object is null
How to Check if an Object Is Null in Java | Delft Stack
February 12, 2024 - This tutorial explains the ways to check whether an object is null or not in Java.
🌐
GeeksforGeeks
geeksforgeeks.org › java › properties-isempty-method-in-java-with-examples
Properties isEmpty() method in Java with Examples - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate // isEmpty() method. import java.util.*; public class GFG { // Main method public static void main(String[] args) { // Create a properties and add some values Properties properties = new Properties(); // Print ...
🌐
Linux Hint
linuxhint.com › check-object-is-null-java
How to Check if an Object is Null in Java
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
CodeGym
codegym.cc › java blog › strings in java › java: check if string is null, empty or blank
Java: Check if String is Null, Empty or Blank
October 11, 2023 - Both strings are null. The String = null The String = Lubaina Khan · “An empty String in Java means a String with length equal to zero.” If a String is empty that means the reference variable is referring to a memory location holding a String of length equal to zero.
Top answer
1 of 3
13

Method overloading can make your implementations more efficient and cleaner:

public static boolean isEmpty(Collection obj) {
    return obj == null || obj.isEmpty();
}

public static boolean isEmpty(String string) {
    return string == null || string.trim().isEmpty();
}

public static boolean isEmpty(Object obj) {
    return obj == null || obj.toString().trim().isEmpty();
}

The Collection version is as efficient as possible.

The String version would be more efficient without the trimming. It would be best to trim your strings as soon you see them, long before they reach this call. If you can review the callers and make sure that the strings are always trimmed at their origins, then you can remove .trim() for best performance.

The Object version can be inefficient, depending on the toString implementation of the objects that will be passed to it, and because of the trimming.

I removed the comparison with null from there, because it seems pointless to me. I mean, a class whose toString method says "null" would seem very very odd.

In any case, you don't really want the Object version to be called, at all. Most importantly because it probably won't even work. Take for example an empty Map. Its toString method returns the string {}, which won't match your conditions of emptiness. (For this type you should definitely add isEmpty(Map<?, ?> map) to benefit from its isEmpty method.)

If performance is so critical, then add more overloaded implementations for all other types that you care about, for example:

public static boolean isEmpty(Something obj) {
    return obj == null || obj.isEmpty();
}

Finally, especially when something is so important, you definitely want to unit test it, for example:

@Test
public void testEmptyObject() {
    assertTrue(isEmpty((Object) null));
    assertFalse(isEmpty(new Object()));
}

@Test
public void testEmptyString() {
    assertFalse(isEmpty("hello"));
    assertTrue(isEmpty(""));
    assertTrue(isEmpty(" "));
    assertTrue(isEmpty((Object) null));
}

@Test
public void testEmptySet() {
    assertFalse(isEmpty(new HashSet<String>(Arrays.asList("hello"))));
    assertTrue(isEmpty(new HashSet<String>()));
}

@Test
public void testEmptyMap() {
    Map<String, String> map = new HashMap<String, String>();
    assertTrue(isEmpty(map));
    map.put("hello", "hi");
    assertFalse(isEmpty(map));
}
2 of 3
9

Don't.

I mean. Don't use the same method for all kinds of objects.

This method does not make much sense to me.

This line smells. A lot.

if (obj instanceof Collection)
    return ((Collection<?>) obj).size() == 0;

Beware of instanceof operator.

I am sure that whatever it is that you are trying to do here, there are better ways to do it.

Java is a statically typed language, use the static types whenever possible. If you really don't know what type the object have, then I will provide another alternative below.


// is below line expensive?
final String s = String.valueOf(obj).trim();

That depends, on the implementation of the object's toString method.

The implementation of String.valueOf is:

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

return s.length() == 0 || s.equalsIgnoreCase("null");

You have already checked for obj == null. The string will only be null when the object's toString method makes it so. And instead of s.length() == 0 you can use s.isEmpty() directly. (Although that is implemented as string length == 0


Do it differently

If possible, have the types of objects you're investigating implement an interface that provides an isEmpty method and let the object decide for itself if it is empty or not.

If that is not possible, you can use a dynamically created map with ways to determine whether or not the object is "empty".

Map<Class<?>, EmptyChecker> map = new HashMap<>();
map.put(String.class, new StringEmptyChecker());
map.put(Point.class, new PointEmptyChecker());

This is a kind of Strategy pattern.

Then to determine if an object is empty:

EmptyChecker checker = map.get(obj.getClass());
checker.isEmpty(obj);

The whole thing is kinda weird though, I can't really see a particular use-case for this kind of method.

🌐
Java2Blog
java2blog.com › home › core java › java basics › check if object is null in java
Check if Object Is Null in Java - Java2Blog
November 29, 2023 - The most basic and efficient way to check if an object is null is by using the == operator. ... Object myObject = null;: This line declares a variable named myObject of the type Object, which is a class in Java.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Check Array Is Null or Empty Example - Java Code Geeks
September 23, 2024 - In this example, I created a simple Java class to check if a given integer or String array is null or empty. I tested the methods with Junit as well as the ObjectUtils.isEmpty and ArrayUtils.isEmpty methods from Apache Common Library.
🌐
Coderanch
coderanch.com › t › 399530 › java › check-Object-empty
How can i check if an Object[][] is empty (Beginning Java forum at Coderanch)
In this case you just check for a null reference: 2) The array has length 0. The check is quite similar to the above: 3) The subarrays are all null or length 0. In this case you have to check each subarray similar to 1 or 2 above. 4) The individual elements in the array are set to null. In this case, you need to check each individual element. So what do you mean by "empty"? Layne ... [I see that Layne posted an answer while I was composing mine, but I think I'll go ahead anyhow. It gives me a warm fuzzy that we had basically the same idea.] There are any number of levels of "empty". Something declared as Object[][] is a reference to an array object that holds M references to array objects that each hold N references to Objects.