Java Object toString()

Java Object toString

What is a Object in Java?

In Java, an object is an instance of a class. A class defines a blueprint for an object, which includes the data (fields or properties) and behavior (methods or functions) of the object. An object is a specific instance of a class, with its own unique set of data and behavior.

  • An object in Java is created using the new keyword, followed by the class name and constructor.

To know more about Object toString in java read the complete article.

Object tostring() method.

The toString() method in Java is a method that is defined in the Object class, which is the parent class of all classes in Java. It returns a string representation of an object. By default, the toString() method returns the name of the class, followed by the object’s memory address in hexadecimal format.

  • The toString() method is often used for debugging and logging purposes, as well as for displaying the state of an object to the user.

Syntax:

public String toString()

Parameters:

The toString() method does not take any parameters.

Let’s look at a object-related Java program where the Java object tostring Method is used to perform an operation on the given string.

Example: Java Object tostring() Method

Run
class Book {
    private String title;
    private String author;
    private int pages;

    public Book(String title, String author, int pages) {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }

    @Override
    public String toString() {
        return title + " by " + author + ", " + pages + " pages";
    }
}

public class Main {
    public static void main(String[] args) {
        Book book1 = new Book("Harry Potter", "J.K. Rowling", 320);
        Book book2 = new Book("The Lord of the Rings", "J.R.R. Tolkien", 456);

        System.out.println(book1.toString());
        System.out.println(book2.toString());
    }
}

Output

Harry Potter by J.K. Rowling, 320 pages
The Lord of the Rings by J.R.R. Tolkien, 456 pages

Example 2 : Java String  substring() Method

Run

class Car {
    private String make;
    private String model;
    private int year;

    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    @Override
    public String toString() {
        return "Car: " + make + " " + model + " (" + year + ")";
    }
}

public class Main {
    public static void main(String[] args) {
        Car car1 = new Car("Honda", "Civic", 2020);
        Car car2 = new Car("Toyota", "Corolla", 2022);

        System.out.println(car1.toString());
        System.out.println(car2.toString());
    }
}

Output

Car: Honda Civic (2020)
Car: Toyota Corolla (2022)

Example 3: how to use the object tostring property:

Run
class Person {
   private String name;
   private int age;

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

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

public class Main {
   public static void main(String[] args) {
      Person person1 = new Person("John", 30);
      Person person2 = new Person("Jane", 25);

      System.out.println(person1.toString());
      System.out.println(person2.toString());
   }
}

Output

Name: John, Age: 30
Name: Jane, Age: 25

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription