3
Sponsored Links


Ad by Google
First lets see how the toString method work internally, then we move why always override the toString method.
toString() method return String representation of the object,default implementation of the toString method in Object class is
public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
 
The return string of the toString() is fully qualified name of the class which the object is an instance,
concatenating with @ symbol and hex string of the hash code return by the hashCode() method.
For example:

Product.java

package com.javamakeuse.test;

public class Product {

 private String name;
 private double price;
 private String manufacturer;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public double getPrice() {
  return price;
 }

 public void setPrice(double price) {
  this.price = price;
 }

 public String getManufacturer() {
  return manufacturer;
 }

 public void setManufacturer(String manufacturer) {
  this.manufacturer = manufacturer;
 }

}
 
Main.java:

package com.javamakeuse.test;

public class Main {

 public static void main(String[] args) {

  Product penDrive = new Product();
  penDrive.setName("IBall 4GB");
  penDrive.setPrice(250);
  penDrive.setManufacturer("HP");

  System.out.println(penDrive);

  System.out.println(penDrive.getClass().getName() + "@"
    + Integer.toHexString(penDrive.hashCode()));
 }
}

output:
com.javamakeuse.test.Product@69adff28
com.javamakeuse.test.Product@69adff28


Both outputs are same first one is printing vi toString() and second one is vi internal implementation of the toString method. The toString method is automatically invoked we don't need to explicitly call the toString(). Whenever we passed the object to print,printf,println() or debugger method it will automatically invoke the toString method.
This is all about how the toString method works, from the above output we can't read what exactly printing this is not a human readable format that's why we need to always override the toString method. We must override the toString method to provide the readable format of the string with proper information. Providing good implementation of toString method makes your class more readable such as providing more pleasant comment on your methods will impress someone to love your code, in programming your code will speaks.
Lets override the toString() method in our Product class, copy and paste the below code into your product class
@Override
 public String toString() {
  return "Product [name=" + name + ", price=" + price + ", manufacturer="
    + manufacturer + "]";
 }
output:
Product [name=IBall 4GB, price=250.0, manufacturer=HP]
com.javamakeuse.test.Product@7e7ee46a

See the differences now toString method returns human readable string with proper information.

Don't forgot to post your comments/feedback/suggestion about this post or if you have something to add on this.
Sponsored Links

3 comments:

  1. Thanks for sharing informative article on java application development. Your post helped to understand the career in Java. Java Training in Chennai

    ReplyDelete
  2. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
    Oracle Training In Chennai

    ReplyDelete
  3. I am reading ur post from the beginning, it was so interesting to read & i feel thanks to you for posting such a good blog, keep updates regularly.Best Hadoop Training Institute In Chennai

    ReplyDelete