Home » Java programming language

ToString Method in Java



In this article, we are going to learn how we can print the value of an object (using ToString method) of a user-defined class?
Submitted by Jyoti Singh, on February 09, 2018

When we create an object of any user-defined class and will print the object, we get the Hexacode address of that Object. See below,

    Book B = new Book ("Include Help");
    System.out.println(B);

Output

@2a139a55

Here, when we will print the B object of class Book we will get the hexacode of the Object rather than the value of the Object.

But, when we do same thing with Java predefined classes such as String. It will not print the address but the value of that object.

    String  S= new String ("Include Help");
    System.out.println(S);

Output

Include Help

So, how it’s happening in case of user-defined class? Isn’t interesting to see what’s happening in the background?

This is because Java Compiler searches for toString() method in every class which converts the object into the string. In case of this method, it is not found in the class it will print the HexaCode Address of Object. But in predefined Java classes like String, toString method is predefined.

If we want same functionality in User-defined classes, we have to declare toString method explicitly inside our class which we have done in our example.

Let’s see this using an example,

Class without defining toString method in our class

package logicProgramming;
//class representing a Book 
class Book
{
	private String Name;
	private String Code;
	private String Author;
	public Book(String Name,String Code,String Author)
	{
		this.Name=Name;
		this.Code=Code;
		this.Author=Author;
	}
	
}
public class ExImportant {

	public static void main(String[] args) {
	Book B=new Book("Let Us C","HT301","Harsh Tomar");	
	
	// If We Use System.out.println(B) It Will Print 
	// The HexaCode Address  of Object B
	// But If We Want to Print the Value Of Object 
	// We have To use toString Method In Our User Defined Class	
	// Let Us UnderStand.........
	System.out.println(B);
	
	// See The Output It Will Print Address Of B	
	
	}
}

Output

[email protected]

Note: logicProgramming is the name of the package and Book is the name of class

Now let’s modify this code to get the value of the Object in order to do this we have to define a toString() method in our class

package logicProgramming;
//class representing the book
class Book
{
	private String Name;
	private String Code;
	private String Author;
	public Book2(String Name,String Code,String Author)
	{
		this.Name=Name;
		this.Code=Code;
		this.Author=Author;
	}
	
	// toString method 
	public String toString()
	{return ("Name :"+this.Name+"\nCode :"+this.Code+"\nAuthor :"+this.Author);}  
}
public class ExImportant2 {

	public static void main(String[] args) {
	Book B=new Book("Let Us C","HT301","Harsh Tomar");	
	// If We Use System.out.println(B) 
	// It Will Print The HexaCode Address  of Object B
	// But If We Want to Print the Value Of Object 
	// We have To use toString Method In Our User Defined Class	
	// Let Us UnderStand.........
	System.out.println(B);
	
	// See The Output It Will Print The Information Of Obeject B	
	
	}

}

Output

Name :Let Us C
Code :HT301
Author :Harsh Tomar





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.