Home »
Java programming language
Java toString() Method
By IncludeHelp Last updated : January 30, 2024
Description (Usage)
Java toString() method returns a string representation of an object.
Syntax
Here are the two syntaxes of Java toString() method:
String toString();
static String toString(type);
Parameter(s)
- value - A value of specific type (such as int)
Return Value
- In the first case, the method returns string representation of an object.
- In the second case, the method returns string value of the given type value.
Example
Here is the example of Java toString() Method:
public class Main {
public static void main(String args[]) {
Integer value = 10;
System.out.println(value.toString());
System.out.println(Integer.toString(108));
// Creating an object of Main class
// Printing it by using toString()
Main obj = new Main();
System.out.println(obj.toString());
}
}
Output
The output of the above example is:
10
108
Main@5fdef03a
Detailed Explanation With More Examples
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
//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 Main {
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
logicProgramming.Book@2a139a55
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
Example
//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 Main {
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