Categories

Sep 3, 2019 in JAVA

Q: tostring method

1 Answer

Sep 4, 2019

Object toString() Method in Java

Every class in java is child of Object class either directly or indirectly. Object class contains toString() method. We can use toString() method to get string representation of an object. Whenever we try to print the Object reference then internally toString() method is invoked. If we did not define toString() method in your class then Object class toString() method is invoked otherwise our implemented/Overridden toString() method will be called.

Syntax of Object class toString() method:

public String toString()

{

      return getClass().getName()+"@"+Integer.toHexString(hashCode());

}

filter_none

edit

play_arrow

brightness_4

// Java program to illustrate 

// working of toString() method 

class Best_Friend { 

    String name; 

    int age; 

    String college; 

    String course; 

    String address; 

    Best_Friend 

    (String name, int age, String college, String course, String address) 

    { 

        this.name = name; 

        this.age = age; 

        this.college = college; 

        this.course = course; 

        this.address = address; 

    } 

public static void main(String[] args) 

    { 

        Best_Friend b =  

        new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu"); 

        System.out.println(b); 

        System.out.println(b.toString()); 

    } 

Output:

[email protected]

[email protected]

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 [email protected]_in_Hexadeciaml_form. If we want the proper information about Best_friend object, then we have to override toString() method of Object class in our Best_Friend class.

filter_none

edit

play_arrow

brightness_4

// Java program to illustrate 

// working of toString() method 

class Best_Friend { 

    String name; 

    int age; 

    String college; 

    String course; 

    String address; 

    Best_Friend 

    (String name, int age, String college, String course, String address) 

    { 

        this.name = name; 

        this.age = age; 

        this.college = college; 

        this.course = course; 

        this.address = address; 

    } 

public String toString() 

    { 

        return name + " " + age + " " + college + " " + course + " " + address; 

    } 

public static void main(String[] args) 

    { 

        Best_Friend b =  

        new Best_Friend("Gulpreet Kaur", 21, "BIT MESRA", "M.TECH", "Kiriburu"); 

        System.out.println(b); 

        System.out.println(b.toString()); 

    } 

Output:

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 overriden for meaningful String representation. Hence, it is highly recommended to override toString() method in our class also.

filter_none

edit

play_arrow

brightness_4

// Java program to illustrate 

// working of toString() method 

import java.util.*; 

class Best_Friend { 

    String name; 

    int age; 

    String college; 

    String course; 

    String address; 

    Best_Friend 

    (String name, int age, String college, String course, String address) 

    { 

        this.name = name; 

        this.age = age; 

        this.college = college; 

        this.course = course; 

        this.address = address; 

    } 

public static void main(String[] args) 

    { 

        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:

[email protected]

Gulpreet Kaur

21

[BIT, M.TECH]

Click here to read more about JAVA
Click here to read more about Insurance

Related questions

Madanswer
Apr 8 in C Sharp (C#)
Sep 2, 2019 in 5G Network
Sep 2, 2019 in 5G Network
...