Understanding toString() method

Home Forums JAVA Understanding toString() method

This topic contains 0 replies, has 1 voice, and was last updated by  admin 8 months, 2 weeks ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #4139

    admin
    Keymaster

    If you want to represent any object as a string, toString() method comes into existence.
    The toString() method returns the string representation of the object.
    If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

    Advantage of the toString() method

    By overriding the toString() method of the Object class, we can return values of the object, so we don’t need to write much code.

    Understanding problem without toString() method

    Let’s see the simple code that prints reference.

    class Student
    {   
          int rollno;   
          String name;
          String city;
          
          Student(int rollno, String name, String city)
          {  
                this.rollno=rollno;
                this.name=name;
                this.city=city;
           }   
        
           public static void main(String args[])
           {   
               Student s1=new Student(101,"Sachin","Dehradun");
               Student s2=new Student(102,"Vijay","Delhi");
               System.out.println(s1);//compiler writes here s1.toString()
               System.out.println(s2);//compiler writes here s2.toString()   
          }   
    }   

    Output: Student@1fee6fc
    Student@1eed786

    As you can see in the above example, printing s1 and s2 prints the hashcode values of the objects but I want to print the values of these objects. Since java compiler internally calls toString() method, overriding this method will return the specified values. Let’s understand it with the example given below:

    Example of toString() method

    Now let’s see the real example of toString() method.

    class Student
    {
         int rollno;
         String name;
         String city;
        
         Student(int rollno, String name, String city)
         {
             this.rollno=rollno;
             this.name=name;
             this.city=city;
         }
            
         public String toString()
        {
             //overriding the toString() method
             return rollno+" "+name+" "+city;
        }
         public static void main(String args[])
        {   
             Student s1=new Student(101,"Raj","Dehradun");
             Student s2=new Student(102,"Vijay","Delhi");    
             System.out.println(s1);//compiler writes here s1.toString()   
             System.out.println(s2);//compiler writes here s2.toString()   
        }    
     }

    Open the Java forum on the website to learn more Java topics!

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.