Object toString() Method In Java

toString() Method in Java | How to Use toString in Java: The Object class is available in Java.lang package, and it is the base for all classes in Java. Every Java class is directly or indirectly derived from the Object class, making it a child of the Object class.

If a class doesn’t extend any other class, it is a direct child of Object. On the other hand, if it extends another class, it is indirectly derived. Consequently, all Java classes have access to the methods of the Object class. The following are the methods of the Java Object class:

toString
equals
clone
wait & notify
getClass
hashCode
finalize

In this first article, we will discuss the toString() method in detail with some real-time examples, and for more Java tutorials for Beginners, you can refer to this link.

toString Method Java Syntax

The syntax of the tostring method java

public String toString() {
      return getClass().getName()+"@"+Integer.toHexString(hashCode());
}

To better understand the default returning value of the toString method java, let us take an example:

What is the toString Java Method?

At first, the toString() method may appear not very useful, and its default implementation is not very helpful because the default toString() method returns a string that includes the class name, followed by an @ sign and a hexadecimal representation of the object’s memory location.

package com.softwaretestingo.string;
public class ToStringMethodEx 
{
	String firstName;
    String lastName;
    int age;
    
    ToStringMethodEx(String firstName,String lastName,int age)
    {
    	this.firstName=firstName;
    	this.lastName=lastName;
    	this.age=age;
    }
	public static void main(String[] args) 
	{
		ToStringMethodEx obj=new ToStringMethodEx("Ramesh", "Dixit",20);
		ToStringMethodEx obj1=new ToStringMethodEx("Anmol", "Arora",30);
		
		System.out.println(obj);
		System.out.println(obj.toString());
		System.out.println(obj1.toString());
	}
}

Output:

com.softwaretestingo.string.ToStringMethodEx@75a1cd57
com.softwaretestingo.string.ToStringMethodEx@75a1cd57
com.softwaretestingo.string.ToStringMethodEx@515f550a

It is important to note that line numbers one and two produce the same output, indicating that when you pass an object instance to methods such as print, println, or loggers, the toString() method is automatically invoked.

If you notice the output, then you can see the class name, and also, we can see the memory address, which is not what we want. But we can get better output by overriding the default implementation of the toString() method and providing something meaningful, like below:

public String toString() {
        return "Person: firstName=" + firstName + ", lastName=" + lastName + ", Age=" + age;
    }

Let’s take an example where we will implement the override toString() method.

Program: Write a Java Program to demonstrate how the toString() method works.
package com.softwaretestingo.string;
public class ToStringMethodEx2 
{
	String firstName;
	String lastName;
	int age;

	ToStringMethodEx2(String firstName,String lastName,int age)
	{
		this.firstName=firstName;
		this.lastName=lastName;
		this.age=age;
	}
	public String toString() 
	{
		return "Person: firstName=" + firstName + ", lastName=" + lastName + ", Age=" + age;
	}
	public static void main(String[] args) 
	{
		ToStringMethodEx2 obj=new ToStringMethodEx2("Ramesh", "Dixit",20);
		ToStringMethodEx2 obj1=new ToStringMethodEx2("Anmol", "Arora",30);

		System.out.println(obj);
		System.out.println(obj.toString());
		System.out.println(obj1.toString());
	}
}

Now let’s check the output:

Person: firstName=Ramesh, lastName=Dixit, Age=20
Person: firstName=Ramesh, lastName=Dixit, Age=20
Person: firstName=Anmol, lastName=Arora, Age=30

Now, if we have compared both outputs, you can notice that the second output is more readable, and anyone can read and understand the output more accurately.

Conclusion:

After reviewing the detailed blog post on the toString() java method, we guess you have all the information and its uses in Java programs. But if you are still struggling with doubts, you can post in the comment section, and we will try to solve that, too.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment