Getting the “identity string” of objects

This short article provides an overview of the various possibilities to obtain the “identity string” for any Java object. This little feature is sometimes very helpful for debugging/logging purposes.

Introduction to the object’s identity string

Java developers often need to print the descriptive string of an object in various ways, for example, on the standard output or using a logging library. I mean something like:

System.out.println("Result: " + anObject);

or

// Consider the Logger from SLF4J
logger.debug("Result: {}", anObject);

These operations are appropriate to do, especially if the object has a “good” toString() method that provides a human-readable description like "Person(firstName=John, lastName=Doe)". However, sometimes you need to obtain the “identity string” of an object, which is a string like "com.example.Person@4e50df2e", even if the object has its good toString() method.

Clearly, the identity string is not helpful for end users, but it can be crucial for developers to check the identity of objects. There are several ways to obtain the identity string, depending on which version of Java you are using and which libraries you have at your disposal.

With Java 19 (or higher)

If you are fortunate enough to use Java 19 (or any later version) in your project(s), the good news is that you can take advantage of the toIdentityString method added to the Objects class.

public static String toIdentityString(Object o)

However, pay attention here because this toIdentityString method is not null-safe. The documentation says it throws a NullPointerException if you pass a null value. Honestly, I don’t understand why null safety was not provided for this method. Indeed, other methods of the same class are null-safe (e.g., equals, hash, and hashCode).

With Apache Commons Lang

The Apache Commons Lang is a well-known “utility” library used by many developers and in many Java projects. Fortunately, this library has an ObjectUtils class that provides 5 identityToString methods.

public static String identityToString(Object object)
public static void identityToString(Appendable appendable, Object object) throws IOException
public static void identityToString(StrBuilder builder, Object object)
public static void identityToString(StringBuffer buffer, Object object)
public static void identityToString(StringBuilder builder, Object object)

The first method is null-safe; passing a null reference returns a null reference (not a string "null"). The other methods, unfortunately, are not null-safe since they throw a NullPointerException if a null is passed for either of the two parameters.

Beware that the identityToString with the StrBuilder parameter is now deprecated and should not be used anymore.

With Spring Framework

If your project uses Spring Framework (any version since 2.0.0), you can use the identityToString method from the ObjectUtils class (it belongs to the spring-core artifact).

public static String identityToString(@Nullable Object obj)

This method is null-safe; if you pass a null reference, it returns an empty ("") string. Just for information, as historical notice, in Spring versions 2.0.0~2.0.5, this method yielded a null (not an empty string) for a null value. Having an empty string for a null may not be so helpful, but it all depends on where you use this value.

A custom object to identity string method

What if you don’t have Java 19 or one of the mentioned libraries? Or if you don’t like the behavior of those available methods? You can quickly implement your own identityToString “utility” method in the following way:

public static String identityToString(Object obj) {
    return obj == null ? null : obj.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(obj));
}

This custom version performs just like the mentioned methods, and, most importantly, it is also null-safe.

Similar Posts