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
🌐
Coderanch
coderanch.com › t › 399530 › java › check-Object-empty
How can i check if an Object[][] is empty (Beginning Java forum at Coderanch)
I wrote this piece of code: Basically i *only* want to create the table, if the Object[][] i get from pd.toObject() is not empty/null. I can't get it to work, i have already tried your suggestions...As for .length() i get a compiler error. I also tried this: if (((Object[][]) pd.toObject())!=null) {.... and it doesnt work. Any further ideas? Correct me if i'm wrong. Thanks for your help ... In your if condition change the condition to: if(pd.toObject()!= null && pd.length!=0) Note: To find the length of an array use its variable called length.
🌐
Educative
educative.io › answers › what-is-objectutilsisempty-in-java
What is ObjectUtils.isEmpty in Java?
System.out.println("The output of ObjectUtils.isEmpty() when an empty set is passed is " + ObjectUtils.isEmpty(input)); ... System.out.println("The output of ObjectUtils.isEmpty() when an object of custom class is passed is " + ObjectUtils....
🌐
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. ... Different types of objects in Java require different approaches to check for null or empty values.
🌐
GeeksforGeeks
geeksforgeeks.org › java › properties-isempty-method-in-java-with-examples
Properties isEmpty() method in Java with Examples - GeeksforGeeks
July 11, 2025 - Java Collection · Last Updated : 11 Jul, 2025 · The isEmpty() method of Properties class is used to check if this Properties object is empty or not. Syntax: public boolean isEmpty() Parameters: This method accepts no parameters Returns: This ...
🌐
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 - ObjectUtils has several useful methods that you can use to check objects and assign default values. Two of them are the isEmpty and isNotEmpty methods. These methods check whether the object is null or empty. if (ObjectUtils.isEmpty(myObject)) ...
🌐
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 - In this tutorial, we’ll learn four approaches to check if all variables of an object are null. The null value in Java means the absence of a variable’s value. Technically, a variable containing null doesn’t point to any position in memory or wasn’t initialized yet. That can only occur with instance variables. Primitive variables such as int, double, and boolean can’t hold null. Checking for null variables in our programs is helpful to avoid unexpected errors like IllegalArgumentException or a NullPointerException.
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.

Find elsewhere
🌐
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.
🌐
Liberian Geek
liberiangeek.net › home › how-to/tips › how to check if an object is null in java?
How to Check if an Object is Null in Java? | Liberian Geek
October 20, 2025 - However, as a recommendation according ... be used. In Java, you can check if an object is null by using the '==' operator to compare the object reference with null....
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-check-if-a-json-object-is-empty-or-not-in-java
How can we check if a JSON object is empty or not in Java?\\n
We can use the isEmpty() method to check if a JSON object is empty or not. It returns true if the JSON object is empty, otherwise, it returns false.
🌐
Javatpoint
javatpoint.com › how-to-check-null-in-java
How to Check null in Java
How to Check null in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Quora
quora.com › What-is-the-correct-way-to-determine-if-Java-object-is-null-or-not-in-tasker
What is the correct way to determine if Java object is null or not in tasker? - Quora
Answer (1 of 2): There are two ways you can do null checking of Java objects. The first way, direct from the userguide, is checking of the object [code ]Equals null[/code]: If A Java object can be directly referenced in a condition. Null-value objects are replaced with text representation [code...
🌐
ServiceNow Community
servicenow.com › community › developer-forum › how-to-check-object-is-empty-or-not › td-p › 2943062
How to check Object is Empty Or not - ServiceNow Community
June 20, 2024 - Hi All, Can you please suggest me how to check Object is empty or not, If JSON is empty then complier should not go till " return JSON.stringify(arr);" if JSON is not empty then complier should touch return JSON.stringify(arr); line in Code. issue: script include returning error message like "Em...
🌐
Quora
quora.com › Is-there-a-better-way-to-do-an-empty-check-in-Java
Is there a better way to do an empty check in Java? - Quora
Answer (1 of 2): You’ve got the Apache StringUtils stuff which has methods like isNotBlank and methods to use a default String. Great for null/empty/space checks on String More generally, there are many ways of conveying empty in Java. None of them are good.
🌐
Medium
medium.com › @malvin.lok › how-java-elegantly-determines-null-or-empty-d809fc136c7d
How Java Elegantly Determines Null or Empty? | by Malvin Lok | Medium
August 8, 2023 - In the actual project, we will have a lot of places where we need to judge the null check, if you do not do to judge the null check may produce NullPointerException exception. First look at the actual project in some of the ways to determine the null way: if (obj == null) { //do something } if (obj2 != null) { //do something } Usually, we determine whether an object is Null or not, we can use Objects.nonNull(obj) in java.util, ObjectUtil in hutool, which will make your code more elegant in some way.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › isEmpty
Java String isEmpty() - Check If Empty | Vultr Docs
December 17, 2024 - Before diving deeper into the applications, it's crucial to understand the basic usage and behavior of the isEmpty() method. This method returns a boolean value, true if the length of the string is zero, and false otherwise.
🌐
W3Schools
w3schools.com › java › ref_string_isempty.asp
Java String isEmpty() Method
String myStr1 = "Hello"; String myStr2 = ""; System.out.println(myStr1.isEmpty()); System.out.println(myStr2.isEmpty()); ... The isEmpty() method checks whether a string is empty or not.
🌐
javaspring
javaspring.net › blog › isempty-java
Mastering `isEmpty()` in Java
For example: import java.util.List; ... { return 0; } int sum = 0; for (int num : list) { sum += num; } return sum; } } The isEmpty() method in Java is a simple yet powerful tool for checking if an object is empty....