Everything You Need To Know About ToString In Java

Last updated on Jun 17,2021 38.1K Views

Everything You Need To Know About ToString In Java

edureka.co

Java is a versatile programming language and has inspired many to pursue its implemenation as a career. People wanting to Java often start with the basics and get lost in the variety concepts it offers. This article on toString in Java will introduce you to a basic but a fairly important topic.

Following are pointers to be discussed in this article,

So let us get started with the first topic of this article,

toString in Java

So what exactly is this method? Object class is the parent class in Java. It contains the toString method. The toString method is used to return a string representation of an object. If any object is printed, the toString() method is internally invoked by the java compiler. Else, the user implemented or overridden toString() method is called.

Here are some of the advantages of using this method.

Advantage

If you override the toString() method of the Object class, it will return values of the object, hence you are not required to write a lot of code.

Example For toString

public class Employee{
int id;
String name;
String city;
Employee(int id, String name, String city){
this.id=id;
this.name=name;
this.city=city;
}
public static void main(String args[]){
Employee e1=new Employee(01,"Ari","NewYork");
Employee e2=new Employee(02,"Jon","Chicago");
System.out.println(e1);//compiler writes here s1.toString()
System.out.println(e2);//compiler writes here s2.toString()
}
}

Output:

Employee@6d06d69c

Employee@7852e922

The code prints the HashCode values of the objects in the example.

Let us try and fine tune our approach in the next part of this article,

Necessary Overriding

Overriding is necessary to return the user specified values:

public class Employee{
int id;
String name;
String city;
Employee(int id, String name, String city){
this.id=id;
this.name=name;
this.city=city;
}
public String toString(){//overriding the toString() method
return id+" "+name+" "+city;
}
public static void main(String args[]){
Employee e1=new Employee(01,"Ari","NewYork");
Employee e2=new Employee(02,"Jon","Chicago");
System.out.println(e1);
System.out.println(e2);
}
}

Output:

1 Ari NewYork

2 Jon Chicago

Hence, this is the procedure to be followed while using the toString method in Java.

Thus we have come to an end of this article on ‘toString in Java’. If you wish to learn more, check out the Java Online Course by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this article  and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework