Easy Tutorial
❮ Method Array Java Enum ❯

Java Object toString() Method

Java Object Class


The Object toString() method is used to return a string representation of the object.

Syntax

object.toString()

Parameters

Return Value

Returns a string representation of the object.

The default format returned is: class name of the object + @ + hexadecimal string of the hashCode.

Example

The following example demonstrates the use of the toString() method:

Example

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

        // toString() with Object
        Object obj1 = new Object();
        System.out.println(obj1.toString());

        Object obj2 = new Object();
        System.out.println(obj2.toString());

        Object obj3 = new Object();
        System.out.println(obj3.toString());
    }
}

The output of the above program is:

java.lang.Object@d716361
java.lang.Object@6ff3c5b5
java.lang.Object@3764951d

Calling the toString() method on an Array class:

Example

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

        // toString() with array
        // Create an array
        String[] array = { "Google", "tutorialpro", "Taobao" };
        System.out.println(array.toString());

        // Return a string representation of the array elements
        // Array inherits from the Object class, so the toString() method can be used directly
        System.out.println(array[1].toString()); // tutorialpro
    }
}

The output of the above program is:

tutorialpro

Java Object Class

❮ Method Array Java Enum ❯