» » » toString() method in java with example program

  • toString() method belongs to object class.
  • By default it will be called once  whenever we try to print object.
  • toString() method by default will print classname@hex representation of hashcode.
  • We can override this toString() method from object class.


  1. public String toString(){
  2.  
  3.    return null;
  4.        
  5. }

Advantage of toString() method in java.

  • When ever we try to print object by default toString() method will be called and print classname@HEX_hashcode.
  • We can represent object in String format using toString() method.
  • if we want to represent string representation of object then we need to override toString() method in our class and return values of the object as a String.




 Java example program to print object without using toString() method.
  • Lets see an example program on printing object of a class.

  1. package tostringexamples;
  2. public class ToStringDemo {
  3.  
  4.     int a,b;
  5.  
  6. ToStringDemo(int x, int y){
  7.         a=x;
  8.         b=y;
  9.  }
  10.  
  11. public static void main(String[] args) {
  12.  
  13.  ToStringDemo obj= new ToStringDemo(1,2);
  14.         
  15.  ToStringDemo obj2= new ToStringDemo(3,4);
  16.         
  17.     System.out.println(obj);
  18.     System.out.println(obj2);
  19.  
  20.  }
  21.  
  22. }


Output:


  1. tostringexamples.ToStringDemo@2a139a55
  2. tostringexamples.ToStringDemo@15db9742

  •  In the above program when we print object of ToStringDemo class it prints ToStringDemo@2a139a55.
  • It means when ever we print object of the class toString() method will be called and by default toString() method print classname@HEX_hashcode.
  • To test this now we will override the toString() method and prints object of the class and if it calls our method then we can understand that when ever we print object by default toString() method will be called.

 Java example program to print object by overriding toString() method.

override tostring method in java example

  • In above example when we print object it executed overridden toString() method.
  • By overriding toString() method we can represent object in string form.

 

toString() method used to convert data to String type. 

  •   By using toString method we can convert any wrapper object to string object
     
Java example program to convert integer to string using toString() method.

  1. package tostringexamples;
  2. public class ToStringDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.   Integer a=12;
  7.   Integer b=37;
  8.  
  9.   String str1=a.toString();
  10.   String str2 =b.toString();
  11.         
  12.   System.out.println(str1);
  13.   System.out.println(str2);
  14.  
  15.     }
  16. }

Output:

  1. 12
  2. 37

Java example program to convert Float to String using toString() method.

  1. package tostringexamples;
  2. public class ToStringDemo {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. Float  a=12.0f;
  7. Float b=33.4f;
  8.  
  9.   String str1=a.toString();
  10.   String str2 =b.toString();
  11.         
  12.   System.out.println(str1);
  13.   System.out.println(str2);
  14.  
  15.     }
  16. }

Output:

  1. 12.0
  2. 33.4

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu