• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo Â» Java Â» Java Tutorial Â» Object toString() Method In Java

Object toString() Method In Java

Last Updated on: July 5, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • toString Method Java Syntax
  • What is toString Java Method?
  • Program: Write a Java Program to demonstrate how toString() method works?

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 Tutorial for Beginners, you can refer to this link.

toString() Method in Java How to Use toString in Java

toString Method Java Syntax

The syntax of the tostring method java

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

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

What is 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, then 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 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.

    Java Programming Languages
    Java Programming Languages Features
    Vector Class In Java
    Vector Class In Java
    Multidimensional Array In Java
    Multidimensional Array In Java
    FileNotFoundException  In Details
    FileNotFoundException in Java Exception Handling And Resolution Example
    Convert String to Char and Char Array in Java
    Convert String to Char and Char Array in Java
    Operator In Java
    Operator In Java
    Java Instanceof in Java
    Instanceof in Java
    Abstract Class In Java
    Abstract Class In Java
    HashMap Class In Java
    HashMap Class In Java
    Continue Java Statement With Examples
    Continue Java Statement With Examples

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers