Java Object toString() Method
The Object toString() method is used to return the string representation of an object.
Syntax
object.toString()
Parameters
None 。
Return Value
Returns the string representation of the object.
Default return format:The object's class name + @ + the hexadecimal string of hashCode。
Example
The following example demonstrates the use of the toString() method:
Example
class ExampleTest {
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 above program produces the following output:
java.lang.Object@d716361 java.lang.Object@6ff3c5b5 java.lang.Object@3764951dOutput format explanation:
- java.lang.Object- Class name
- @- Symbol
- d716361- Hexadecimal value of the hash code
The Array class calls the toString() method:
Example
class ExampleTest {
public static void main(String[] args) {
// toString() with array
//Creating an array
String[] array = { "Google", "Example", "Taobao" };
System.out.println(array.toString());
//Returns a string representation of the array element values
//Array inherits from the Object class, so the toString() method can be used directly.
System.out.println(array[1].toString()); // Example
}
}
The above program produces the following output:
Example
Other extensions
Java Object Class