Object Identity Strings in Java (with Examples)

In Java programming, an identity string is a unique identifier associated with an object instance without calling the overridden toString() or hashCode() methods. It helps in distinguishing one object from another, even if they have the same content or state.

1. The Default Behavior with and without toString() Method

By default, if we have not overridden the toString() method, Java returns a string in the format ‘ClassName@HashCode‘ when we print the object. This string is referred to as an object identity string.

Consider the following Java class Record which has two fields.

class Record {

  long id;
  String name;

Notice that we do not have overridden the toString() method. Let us try to print this object.

Record record = new Record(1L, "record name");

The program output:


As we do in regular programming, now lets add the toString() method that prints the record details.

class Record {

  public String toString() {
    return STR."Record{id=\{id}, name='\{name}\{'\''}\{'}'}";

Now when we run the previous program again, this time we will get the record details printed in the console.

Record{id=1, name='record name'}

2. Accessing the Object Identity String

2.1. Java 19 and Later

Starting with JDK 19, we can obtain the object identity string with the method: Objects.toIdentityString().

Let us run this method with our Record class instance having the toString() method.

Record record = new Record(1L, "record name");

System.out.println(record);  //toString is called
System.out.println(Objects.toIdentityString(record));  //toString is NOT called

The program output:

Record{id=1, name='record name'}

Note that the hashcode has changed to the addition of toString() method, when comparing to hashcode in the first example.

This is worth noting that Objects.toIdentityString method is not null-safe so be careful when using it else you will get the deadly NullPointerException.

System.out.println(record != null ? Objects.toIdentityString(record) : "null");

2.2. Java 18 and Before

For applications running on previous versions, we can build the identity string from an object without calling the overridden toString() or hashCode() as follows:

static String getIdentityString(Object object) {
  return object.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(object));

Now when we use this method, we get the object identity printed into the console.


The program output:


3. Why do We Need the Object Identity String?

Identity strings are particularly useful in scenarios where object equality based on content is not sufficient, such as in debugging (object tracking), distributed systems, or caching.

For example, two Java objects may have the same state, but if they are separate instances, they will have different identities. This can aid the Java developers in identifying and tracing the lifecycle of objects within a Java application. This can be especially useful for identifying memory leaks or unintended object retention.

Similarly, when serializing and deserializing objects in a distributed system, identity strings can be included in the message payload to ensure the correct identification of objects on the receiving end.

4. Conclusion

This short Java tutorial discussed what is object identity strings, and the method Objects.toIdentityString() (added in Java 19) to print the identity string of an object. We also learned to print the identity string in Java 18 and prior versions.

It should be kept in mind that the toIdentityString() is not null-safe and may throw the NullPointerException so use it with care.

Happy Learning !!

Source Code on Github


Notify of
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.