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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.