1. toString() Method in Java
The toString() method is used to return the string representation of an object.
It helps convert object data into readable text format.
The toString() method belongs to the Object class, so every Java class automatically inherits it.
2. Why toString() Method is Used
The toString() method is mainly used to display meaningful object information.
- a. Convert objects into readable text
- b. Print object details easily
- c. Help debugging
- d. Improve output readability
- e. Log object information
3. Where toString() Method is Used
The toString() method is used in many real-world Java applications.
- a. Logging systems
- b. Debugging applications
- c. Printing object details
- d. API responses
- e. Student management systems
- f. Employee management systems
4. Default Behavior of toString()
If a class does not override toString(), Java uses the default implementation from the Object class.
4.1 Example of Default toString()
class Student {
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.toString());
}
}
The output contains the class name and hexadecimal hash code.
4.2 Understanding Default Output
| Part | Meaning |
| Student | Class name |
| @ | Separator symbol |
| 3f99bd52 | Hash code in hexadecimal |
The default toString() output is usually not user-friendly.
5. Overriding toString() Method
Developers usually override toString() to return meaningful object information.
5.1 Basic Overriding Example
class Student {
int id = 101;
String name = "Rahul";
public String toString() {
return id + " " + name;
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s);
}
}
Java automatically calls toString() when the object is printed.
System.out.println(object) automatically calls object.toString().
6. Internal Working of toString()
Internally, Java converts the returned value from toString() into printable text.
6.1 Automatic Method Call
System.out.println(student);
Internally, Java converts this statement into:
System.out.println(student.toString());
The returned String value is then displayed on the screen.
6.2 toString() Must Return String
The method always returns a String object.
public String toString() {
return "Java";
}
Returning any non-String value directly is not allowed.
7. toString() with Multiple Fields
The method can return multiple object properties together.
7.1 Example with Multiple Variables
class Employee {
int id;
String name;
double salary;
Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public String toString() {
return id + " " + name + " " + salary;
}
}
public class Main {
public static void main(String[] args) {
Employee e = new Employee(1, "Amit", 50000);
System.out.println(e);
}
}
The output displays all object details in readable format.
8. Relationship Between Object Class and toString()
Every Java class directly or indirectly inherits from the Object class.
Because of this inheritance, every class automatically gets the toString() method.
8.1 Simplified Object Class Method
public String toString() {
return getClass().getName() + "@" +
Integer.toHexString(hashCode());
}
This is the basic idea behind the default implementation of toString().
9. String Conversion Behavior
Java automatically converts objects into Strings in many situations.
9.1 Concatenation with String
class Student {
int id = 101;
public String toString() {
return "Student ID: " + id;
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
System.out.println("Details: " + s);
}
}
Java automatically calls toString() during String concatenation.
9.2 Printing Collections
Collections internally call toString() for their stored objects.
import java.util.ArrayList;
class Student {
int id;
Student(int id) {
this.id = id;
}
public String toString() {
return "Student ID: " + id;
}
}
public class Main {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add(new Student(1));
list.add(new Student(2));
System.out.println(list);
}
}
Each object inside the collection is converted into readable text using toString().
Well-written toString() methods improve debugging and logging readability.
10. Common Beginner Mistakes
10.1 Forgetting Method Signature
The method name and return type must match exactly.
public string toString() {
return "Java";
}
This code is incorrect because Java uses String, not string.
10.2 Returning Non-Readable Output
public String toString() {
return "";
}
Returning empty or unclear text reduces usefulness.
10.3 Forgetting @Override Annotation
The @Override annotation helps detect mistakes during compilation.
@Override
public String toString() {
return "Student Object";
}
The annotation confirms that the parent method is correctly overridden.
Without proper overriding, Java uses the default Object class implementation.
11. Edge Cases in toString()
11.1 Returning null
public String toString() {
return null;
}
Returning null is technically allowed but not recommended because it can create confusing output.
11.2 Recursive toString() Calls
Careless object references can create infinite recursion.
class A {
B b;
public String toString() {
return b.toString();
}
}
class B {
A a;
public String toString() {
return a.toString();
}
}
This type of circular reference can cause StackOverflowError.
12. Important Points to Remember
- a. toString() belongs to the Object class
- b. Every Java class inherits toString()
- c. System.out.println(object) automatically calls toString()
- d. Overriding improves readability
- e. toString() must return a String
- f. Collections internally use toString()
- g. @Override helps avoid mistakes
- h. Recursive object references can cause StackOverflowError
The Java toString() method is an important part of object-oriented programming because it allows objects to be represented in readable text form. Properly overriding toString() improves debugging, logging, and application readability while making object data easier to understand and manage.
