Object toString() Method in Java
Last Updated :
04 Apr, 2025
Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class, henceforth, it is a child of the Object class. If a class does not extend any other class then it is a direct child class of Object, and if it extends another class, then it is indirectly derived. Therefore the Object class methods are available to all Java classes.
Note: The Object class acts as the root of the inheritance hierarchy in any Java program.

Now we will be dealing with one of its methods, known as the toString() method. We typically do use the toString() method to get the string representation of an object. It is very important and readers should be aware that whenever we try to print the object reference, then internally toString() method is invoked. If we did not define the toString() method in our class, then the Object class toString() method is invoked; otherwise, our implemented or overridden toString() method will be called.
Syntax:
public String toString() {
return getClass().getName()+"@"+Integer.toHexString(hashCode());
}
Note: Default behavior of toString() is to print class name, then @, then unsigned hexadecimal representation of the hash code of the object.
Example: Default behaviour of the object.toString() method.
JAVA
// Java program to Illustrate
// working of toString() method
public class Geeks
{
String name;
String age;
Geeks(String name, String age) {
this.name = name;
this.age = age;
}
// Main Method
public static void main(String[] args)
{
Geeks g1 = new Geeks("Geeks", "22");
// printing the hashcode of the objects
System.out.println(g1.toString());
}
}
Output explanation: In the above program, we create an Object of Geeks class and provide all the information of a friend. But when we try to print the Object, then we are getting some output which is in the form of classname@HashCode_in_Hexadeciaml_form. If we want the proper information about the Geeks object, then we have to override the toString() method of the Object class in our Geeks class.
Example 2: Overriding the toString() method to enhance the output formatting.
Java
// Java program to Illustrate
// working of toString() method
public class Geeks
{
String name;
String age;
Geeks(String name, String age) {
this.name = name;
this.age = age;
}
// Overriding toString() method
@Override
public String toString() {
return "Geeks object => {" +
"name='" + name + '\'' +
", age='" + age + '\''+
'}'
;
}
// Main Method
public static void main(String[] args)
{
Geeks g1 = new Geeks("Geeks", "22");
// printing the hashcode of the objects
System.out.println(g1.toString());
}
}
OutputGeeks object => {name='Geeks', age='22'}
Note: In all wrapper classes, all collection classes, String class, StringBuffer, StringBuilder classes toString() method is overridden for meaningful String representation. Hence, it is highly recommended to override toString() method in our class also.
Similar Reads
Overriding toString() Method in Java Java being object-oriented only deals with classes and objects so do if we do require any computation we use the help of object/s corresponding to the class. It is the most frequent method of Java been used to get a string representation of an object. Now you must be wondering that till now they wer
3 min read
StringJoiner toString() method in Java The toString() of StringJoiner is used to convert StringJoiner to String. It returns the current value, consisting of the prefix, the values added so far separated by the delimiter, and the suffix, unless no elements have been added in which case, the prefix + suffix or the emptyValue characters are
2 min read
Boolean toString() Method in Java In Java, the toString() method of the Boolean class is a built-in method to return the Boolean value in string format. The Boolean class is a part of java.lang package. This method is useful when we want the output in the string format in places like text fields, console output, or simple text forma
2 min read
Hashtable toString() Method in Java The toString() method in the Hashtable class in Java returns a string representation of the key-value mappings contained in the hashtable. The string is in the form of a comma-separated list of key-value pairs enclosed in curly braces. The toString() method is inherited from the java.util.Hashtable
2 min read
JavaTuples toString() method The toString() method in org.javatuples is used to convert the values in TupleClass into a String. This method is inherited from the JavaTuple class. This method can be used to any tuple class object of javatuples library. It returns a String value formed with the values in the TupleClassObject. Met
2 min read
Method Class | toGenericString() method in Java The java.lang.reflect.Method.toGenericString() method of Method class returns a string which gives the details of Method, including details of type parameters of the method. Syntax: public String toGenericString() Return Value: This method returns a string which gives the details of Method, includin
3 min read