how to use toString method in java

You are here : Home / Core Java Tutorials


Contents of page >
  • java. toString method exists in which class in java?
  • toString method exists in which class in java?
  • What is purpose of toString() method in java?
  • Which java api classes override toString() method in java?
  • Program without toString implementation in java >
  • Program/Example with toString implementation in java >

In this java tutorial we will learn about toString method in java. toString method exists in which class in java?
What is purpose of toString() method in java?
Which java api classes override toString() method in java? Program without toString implementation and program with toString implementation in java.
toString method exists in which class in java?
toString method exists in java.lang.Object class. As all classes extends java.lang.Object class by default in java so toString method becomes available to all the classes in java.


What is purpose of toString() method in java?
toString() method is used to give the String representation of any object in java in java.


Which java api classes override toString() method in java?
String, Integer, all the Collection Api classes override toString() method in java.

Program without toString implementation in java >
class Employee{
   String name;
   String id;
  
   public Employee() {}
  
   public Employee(String name, String id) {
       this.name = name;
       this.id = id;
   }
  
   //Don't Override the toString method
  
}
public class WithoutToStringMethodImplementation {
   public static void main(String[] args) {
         
          Employee emp = new Employee("sam", "1");
         
          System.out.println(emp);
   }
}
/* output
toString_.Employee@659e0bfd
*/


Compare the output of above program without toString implementation and Program with toString implementation in java.


Program/Example with toString implementation in java >
class Employee{
   String name;
   String id;
  
   public Employee() {}
  
   public Employee(String name, String id) {
       this.name = name;
       this.id = id;
   }
  
  
   @Override
   //Override the toString method
   public String toString() {
       return "Employee{" + "name=" + name + ", id=" + id  + '}';
   }
  
}
public class ToStringMethodExample {
   public static void main(String[] args) {
         
          Employee emp = new Employee("sam", "1");
         
          System.out.println(emp);
   }
}
/* output
Employee{name=sam, id=1}
*/


SUMMARY >
So in this java tutorial we learned about toString method in java. toString method exists in which class in java?
What is purpose of toString() method in java?
Which java api classes override toString() method in java? Program without toString implementation and program with toString implementation in java.

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>






Collection - List, Set and Map all properties in tabular form




eEdit
Must read for you :