The Artima Developer Community
Sponsored Link

Java Buzz Forum
toString() method in java with example program

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
toString() method in java with example program Posted: Jun 10, 2016 11:31 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: toString() method in java with example program
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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.

    Read: toString() method in java with example program

    Topic: Git Delete Remote Branch Example Previous Topic   Next Topic Topic: Highly Rated, Dramatically Discounted Adapters - Deal Alert

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use