Java Utility Library

Java Objects - toString() Method



The java.util.Objects.toString() method returns the result of calling toString for a non-null argument and "null" for a null argument.

Syntax

public static String toString(Object o)

Parameters

o Specify an object.

Return Value

Returns the result of calling toString for a non-null argument and "null" for a null argument.

Exception

NA

Example:

In the example below, the java.util.Objects.toString() method returns a string representation of the given object.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating Objects
    Object obj1 = 100;
    Object obj2 = null;

    //calling toString method for objects
    System.out.println("obj1 is: " + Objects.toString(obj1));
    System.out.println("obj2 is: " + Objects.toString(obj2));
  }
}

The output of the above code will be:

obj1 is: 100
obj2 is: null

❮ Java.util - Objects