Why we need to override toString() in java

tostring override in java

Why we need to override toString in java:

toString() will generally provide the string representation of the object. In order to provide an additional information for the property/field of an object we have to override the toString() method. Continue reading to the full article to understand why we need, when to use and how to use toString() methods in java.

 

What is toString() method ?

toString() is also one of Object class method used to return the string representation of the object/Integer/any Wrapper class objects. Java compiler invokes the toString() method when you print any object, so overriding toString() method will help for Debugging/Logging/any string manipulations like null check/concat etc..

 

If the value is Integer or Long or Double(only wrapper class objects) then it will convert to string from Integer or Double or Long using toString() method,

package in.javadomain;

public class toToString {

	public static void main(String[] args) {
		Integer j = 20;
		Double d = 20.24;
		Long l = 150L;
		System.out.println(j.toString());
		System.out.println(d.toString());
		System.out.println(l.toString());
	}

}

Output:

20
20.24
150

 

String representation of the object ?

Yeah. Calling toString() method/printing any object will return,


"ClassName@Integer.toHexString(hashcode())"


ClassName ==> getClass().getName() (or) your current execution class name.

Eg: in.javadomain.BlogAuthor@1f33675

here,

BlogAuthor => Class Name

 

in.javadomain = > Package name

 

1f33675 ==> unsigned hexadecimal representation of the hash code of the object

 

 

Why toString() method ?

toString() method has given to provide a complete information about the object in textual/string representation. But the default implementation[ClassName@hashcode()] will not help us really to get all the information’s. So we are overriding this to provide the understandable information’s of an object.

In the below example, we are overriding toString() method to print the objects field/property values. Which help us to debug the things. [Like to see the exact property/field values of an object]

 

What is the real meaning of overriding toString() method ?

overriding toString() method is customizing the string representation of the object rather than just printing the default implemenation.

 

When to use toString() method ?

you can use for Debugging/Logging/any null checks/concatinations/any string manipulations prior to printing an object’s attribute.

 

Without overriding toString() [Default implementation]:


package in.javadomain;

public class BlogAuthor {

String blogName;
String authorName;

BlogAuthor(String blogName, String authorName) {
this.blogName = blogName;
this.authorName = authorName;
}

public static void main(String[] args) {
BlogAuthor blogAuthor = new BlogAuthor("Javadomain.in", "Naveen");
System.out.println(blogAuthor);
}

}

 

Output:

in.javadomain.BlogAuthor@1f33675

 

 

Overriding toString() [To print the object property values]:

 

package in.javadomain;

public class BlogAuthor {

String blogName;
String authorName;

BlogAuthor(String blogName, String authorName) {
this.blogName = blogName;
this.authorName = authorName;
}
@Override
public String toString() {
return authorName+" is the author of "+blogName;
}

public static void main(String[] args) {
BlogAuthor blogAuthor = new BlogAuthor("Javadomain.in", "Naveen");
System.out.println(blogAuthor);
}

}

 

Output:

Naveen is the author of Javadomain.in

 

Note:

Here without overriding toString() method, prints an object as className@hashcode which is not providing sufficient information’s about an object, but overrided toString() provides the field/attribute values of an object.

 

1,154 total views, 1 views today

About: Mirthbees

Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in


2 thoughts on “Why we need to override toString() in java”

    1. Prerequisites to use Wrapper class ?

      Can you be a bit clear on what exactly you mean ?

      Wrapper class we use when we need any methods, say you want to convert integer to string then

      int i =10; [primitive here, so you can’t get any method]

      but for wrapper classes Integer i =10; [you can get many wrapper class methods for many utility achievements]

      Also, collections will allow only wrapper classes. But still you are allowed to store the primitive values. It will be autoboxed internally.

Leave a Reply

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