Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class, henceforth it is a child of the Object class. If a class does not extend any other class then it is a direct child class of Object and if extends another class then it is indirectly derived. Therefore the Object class methods are available to all Java classes.
Note: Object class acts as a root of inheritance hierarchy in any java program
Now we will be dealing with one of its methods known as toString() method. We typically generally do use the toString() method to get the string representation of an object. It is very important and readers should be aware that whenever we try to print the object reference then internally toString() method is invoked. If we did not define the toString() method in your class then the Object class toString() method is invoked otherwise our implemented or overridden toString() method will be called.
Syntax:
public String toString() { return getClass().getName()+"@"+Integer.toHexString(hashCode()); }
Note: Default behavior of toString() is to print class name, then @, then unsigned hexadecimal representation of the hash code of the object.
Example
JAVA
// Java program to Illustrate // working of toString() method // Main class class Best_Friend { // Member attributes of this class String name; int age; String college; String course; String address; // Constructor of this class Best_Friend(String name, int age, String college, String course, String address) { // This variable refers to current instance itself this .name = name; this .age = age; this .college = college; this .course = course; this .address = address; } // Method of this class // Main driver method public static void main(String[] args) { // Creating an object of this class // Custom attributes been passed as in arguments Best_Friend b = new Best_Friend( "Gulpreet Kaur" , 21 , "BIT MESRA" , "M.TECH" , "Kiriburu" ); // Print and display commands to illustrate // toString() method as both will print the same // Print the object System.out.println(b); // Print the object but implicitly using toString() // method System.out.println(b.toString()); } } |
Best_Friend@3d075dc0 Best_Friend@3d075dc0
Output explanation: In the above program, we create an Object of Best_Friend class and provide all the information of a friend. But when we try to print the Object, then we are getting some output which is in the form of classname@HashCode_in_Hexadeciaml_form. If we want the proper information about the Best_friend object, then we have to override the toString() method of the Object class in our Best_Friend class.
Example 2:
JAVA
// Java program to illustrate // working of toString() method // Main class class Best_Friend { // Member attributes of this class String name; int age; String college; String course; String address; // Constructor of this class Best_Friend(String name, int age, String college, String course, String address) { // This keyword refers to current instance itself this .name = name; this .age = age; this .college = college; this .course = course; this .address = address; } // Method 1 // Creating our own toString() method public String toString() { return name + " " + age + " " + college + " " + course + " " + address; } // Method 2 // Main driver method public static void main(String[] args) { // Creating object of class inside main() method Best_Friend b = new Best_Friend( "Gulpreet Kaur" , 21 , "BIT MESRA" , "M.TECH" , "Kiriburu" ); // Print and display commands to illustrate // toString() method as both will print the same // Print the object System.out.println(b); // Printing object but using toString() method System.out.println(b.toString()); } } |
Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu Gulpreet Kaur 21 BIT MESRA M.TECH Kiriburu
Note: In all wrapper classes, all collection classes, String class, StringBuffer, StringBuilder classes toString() method is overridden for meaningful String representation. Hence, it is highly recommended to override toString() method in our class also.
Example 3:
JAVA
// Java program to illustrate // working of toString() method // Importing all utility classes import java.util.*; // Main class class Best_Friend { // Member attributes of this class String name; int age; String college; String course; String address; // Constructor of this class Best_Friend(String name, int age, String college, String course, String address) { // This keyword refer to current instance itself this .name = name; this .age = age; this .college = college; this .course = course; this .address = address; } // Method of this class public static void main(String[] args) { // Creating an object of class in main() method Best_Friend b = new Best_Friend( "Gulpreet Kaur" , 21 , "BIT MESRA" , "M.TECH" , "Kiriburu" ); System.out.println(b); String s = new String( "Gulpreet Kaur" ); System.out.println(s); Integer i = new Integer( 21 ); System.out.println(i); ArrayList l = new ArrayList(); l.add( "BIT" ); l.add( "M.TECH" ); System.out.println(l); } } |
Output: It will also do throw out for warning of unchecked and unsafe operations
Best_Friend@232204a1 Gulpreet Kaur 21 [BIT, M.TECH]
This article is contributed by Bishal Kumar Dubey. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.