Home » Java programming language

Java Object Class String toString() method with Example

Java Object class String toString() method: Here, we are going to learn about the String toString() method of Object class with its syntax and example.
Submitted by Preeti Jain, on June 28, 2019

Object Class String toString()

  • This method is available in java.lang.Object.toString().
  • This method is used to return the string representation of the object.
  • This method returns a string of the object that is easy to understand for the end user.
  • This method does not return an exception.

Syntax:

    String toString(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the Object class.

Return value:

The return type of this method is String that means this method returns String representation of the object.

Java program to demonstrate example of Object Class toString() method

import java.lang.Object;
import java.util.LinkedList;

public class ObjectClass {
    public static void main(String[] args) {

        // Create an object of Float type

        Float fl = new Float(10.f);

        // Create an object of linked list type

        LinkedList ll = new LinkedList();

        // Add few elements in Linked List

        ll.add(10);
        ll.add(20);

        // Display String representation of the objects

        System.out.println("The String representation of the Object fl is: " + fl.toString());
        System.out.println("The String representation of the Object ll is: " + ll.toString());
    }
}

Output

D:\Programs>javac ObjectClass.java

D:\Programs>java ObjectClass
The String representation of the Object fl is :10.0
The String representation of the Object ll is :[10, 20]



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.