chevron_left
354 points
12 7 5

Before proceeding with this article I would suggest you to go through the below article which gives a brief description about the Object class.

Java's Object class

Object class is the super class of all the classes. Hence, all the methods of this classes are implicitly inherited to any class. There are few methods of this class which can be overridden. toString() is one of those methods which can be overridden. Howsoever the default implementation of this method may be inadequate for some classes and is overridden to ameliorate the object representation. It is recommended that all the subclasses override this method.

The syntax of toString() method:

String toString() - Returns a string that textually represents the object that is human readable and appropriately describe an object of the class. The default method returns a string consisting the name of the class for which the object is instantiated, concatanated with @ sign and hash code of the object. Finally the return string looks like below

getClass().getName() + '@' + Integer.toHexString(hashCode())

A simple code to show object is represented with default toString() method :

public class ToStringDemo {

public static void main(String[] args) {
	ToStringDemo obj=new ToStringDemo();
	System.out.println(obj);
}

}

output:
ToStringDemo@36aa7bc2

If we interpret this output, It returns the string which is the combination of the class name + @ + hashcode

Now let us see how we can override this method to improve the object representation

public class ToStringDemo {
@Override
public String toString()
{
	return "This is ToStringDemo class";
	
}
public static void main(String[] args) {
	ToStringDemo obj=new ToStringDemo();
	System.out.println(obj);
}

}

The output of this code:
This is ToStringDemo class

In the preceding example, toString() method is overridden and a string value is returned. Whenever an obj is represented with print(), println() statements it automatically invokes the toString() method which displays the return value of this method. Any object which is instantiated for this class will return the value This is ToStringDemo class

ToStringDemo obj=new ToStringDemo();
ToStringDemo obj1=new ToStringDemo();
System.out.println(obj);
System.out.println(obj1);

returns

This is ToStringDemo class
This is ToStringDemo class

1 Comment

You try to make Java as easy as it is difficult. Keep up the good work. Good luck to you. Continue to post:))

More Posts

How to make a tostring method in java amna - Jul 28
What is the use of tostring method in java offonime - Jun 18
If we override the toString() method with the code below, what would be the result of printing? Farkhanda Athar - Aug 9, 2020
Attempt to invoke virtual method 'int.java.lang.Integer.intValue()' on a null object reference at Cast.writetoParce Sabbir Hossain - Mar 31
Attempt to invoke virtual method on a null object reference Hasnain_khan - Oct 9, 2020
Object class in Java DivyaN - Sep 11
Java class and object Yogesh Sharma - Aug 20
How to call an object from another class in java? Hafsa_Rao - Aug 18
Can we create object of abstract class in java sakshi - Jun 2
Java - System class arraycopy() method DivyaN - Sep 7