The toString method is used to return the string representation of the object on which this method is invoked. Before going into implementation details of toStrimg method, let us first memorize some important points about toString method:

  • The return type of toString method is a string object.
  • The Object class in java has defined this method and hence is inherited by all classes in jdk and user classes.
  • The default implementation of the toString method returns the hashcode of the object on which this method is invoked.
  • It is recommended to override the toString method in every class you write. The purpose of this method should be to return a meaningful representation of the object. This helps anyone to see the value held by the object. For e.g. the ArrayList class’s toString method returns a string which shows the elements of the ArrayList.
  • As you may be aware that System.out.println method can accept argument of any type and it prints the string representation of the argument on the console.

The best practices to remember while overriding the toString method include the following points:

  • The value returned by this method should return the most significant representation of the object.
  • We can skip overriding the toString method when the object doesn’t have any state associated with it. For e.g. a helper class for performing common string operations.
  • When overriding this method, do include any properties inherited from the super class.
  • The source code for a sample implementation of the toString method is shown below:
package com.example;

public class Square {

	int side;
	int area;
	Square(int side) {
		this.side = side;
		this.area = side*side;
	}

       @override
	public String toString() {
		return "Side is " + side + " and area is " +
				area;
	}
	
	public static void main(String args[]) {
		Square s = new Square(2);
		System.out.println(s);
	}
}

Output:

Side is 2 and area is 4

As shown in the above code, the toString method of class “Square” is printing the length of side and the area of Square. In real world, the toString method can get more complex than the one shown above. The only thing to understand when implementing the toString method in a Class is to provide relevant information with good presentation.

The modern IDE’s like Netbeans and Eclipse have added the functionality to generate a sample code for toString method and add it to the current class. We may use that too but we also need to customize the code generated by these IDE’s.

Related Posts

How to override toString method in Java admin Core Java
The toString method is used to return the string representation of the object on which this method is invoked. Before going into implementation details of toStrimg method, let us first memorize some important points about toString method: The return type of toString method is a string object. The Object class in...
The toString method is used to return the string representation of the object on which this method is invoked. Before going into implementation details of toStrimg method, let us first memorize some important points about toString method: <ul> <li>The return type of toString method is a string object.</li> <li>The Object class in java has defined this method and hence is inherited by all classes in jdk and user classes.</li> <li>The default implementation of the toString method returns the hashcode of the object on which this method is invoked.</li> <li>It is recommended to override the toString method in every class you write. The purpose of this method should be to return a meaningful representation of the object. This helps anyone to see the value held by the object. For e.g. the ArrayList class's toString method returns a string which shows the elements of the ArrayList.</li> <li>As you may be aware that System.out.println method can accept argument of any type and it prints the string representation of the argument on the console.</li> </ul> The best practices to remember while <a href="http://www.javaexperience.com/overriding-compareto-method-in-java/" title="Overriding compareTo() method in Java">overriding</a> the toString method include the following points: <ul> <li>The value returned by this method should return the most significant representation of the object.</li> <li>We can skip overriding the toString method when the object doesn't have any state associated with it. For e.g. a helper class for performing common string operations.</li> <li>When overriding this method, do include any <a href="http://www.javaexperience.com/multiple-inheritance-in-java/" title="Multiple inheritance in Java">properties inherited from the super class</a>.</li> <li>The source code for a sample implementation of the toString method is shown below:</li> </ul> 1 Output: <blockquote> Side is 2 and area is 4 </blockquote> As shown in the above code, the toString method of class "Square" is printing the length of side and the area of Square. In real world, the toString method can get more complex than the one shown above. The only thing to understand when implementing the toString method in a Class is to provide relevant information with good presentation. The modern IDE's like Netbeans and Eclipse have added the functionality to generate a sample code for toString method and add it to the current class. We may use that too but we also need to customize the code generated by these IDE's.
wait and sleep methods difference in Java
this and super for constructor chaining in Java
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Latest posts by admin (see all)

Comments

comments