Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Video lesson

Understanding a toString() Method in Java

In this lesson, you will learn

  • Understanding toString() Method in Java
  • Example

 

Understanding toString() Method in Java

The toString() method in Java is a special method defined in the Object class, which is the ultimate superclass of all classes in Java.

Every class you create implicitly inherits the toString() method (or can override it). Its primary purpose is to provide a string representation of an object.

 

Default Behavior:

If you don’t override the toString() method in your class, you’ll inherit the default implementation from the Object class. This default implementation typically returns a string consisting of the class name, an “@” symbol, and the object’s hash code (a unique integer identifier for the object). This default string representation is often not very informative.

Overriding toString():

The real power of toString() comes from overriding it in your classes. By overriding it, you can customize the string representation to provide meaningful information about the object’s state. This makes it much easier to print or display object data in a user-friendly way.

 

Example: toString() Method

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Overriding the toString() method
    @Override
    public String toString() {
        return "Name: " + name + ", Age: " + age;
    }

    // Getters (for demonstration purposes)
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

}

public class ToStringExample {
    public static void main(String[] args) {
        Person person = new Person("Alice", 30);

        // Implicit call to toString() when printing the object
        System.out.println(person); // Output: Name: Alice, Age: 30

        // Explicit call to toString()
        String personString = person.toString();
        System.out.println(personString); // Output: Name: Alice, Age: 30

        // Demonstrating what happens if you DON'T override toString()
        Person person2 = new Person("Bob", 25);
        System.out.println(person2); // Output (something like): Person@76ed5528 (not very useful)

    }
}


Explanation:

  1. Person Class: The Person class has name and age fields.

  2. toString() Override: The @Override annotation is optional but good practice. It tells the compiler you intend to override a method. The toString() method is overridden to return a formatted string containing the person’s name and age.

  3. Printing the Object: In the main method, when you try to print the person object using System.out.println(person), Java implicitly calls the toString() method of the Person object. Because we’ve overridden it, we get the custom string representation (“Name: Alice, Age: 30”).

  4. Explicit Call: You can also call toString() explicitly: String personString = person.toString();.

  5. Without Override: The person2 object demonstrates what happens if you don’t override toString(). You’ll get the default, less informative string from the Object class.

Key Points:

  • toString() is inherited from the Object class.
  • Overriding toString() lets you customize the string representation of your objects.
  • System.out.println(object) implicitly calls object.toString().
  • Overriding toString() makes debugging and logging much easier. It’s a very common and important practice in Java. It’s almost always a good idea to override toString() in your classes to provide a useful string representation.

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review