-3
What is the function of  toString in here?
what is the need for toString

can anyone please explain what toString suppose to do here. I am new in java and learning alot of new stuff

public class Employee  
    {
       private String name;
       private String address;
       private int number;
       public Employee(String name, String address, int number)
       {
          System.out.println("Constructing an Employee");
          this.name = name;
          this.address = address;
          this.number = number;
       }




     public String toString() //what is  this function doing
       {
          return name + " " + address + " " + number;
       }

Heading

Mohammad Hemel
  • 1
  • 1
  • 1
  • 1
  • Read this: http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java – Pradeep Simha Oct 29 '13 at 03:08
  • -1 because you could have found it in [documentation](http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#toString()) – Ean V Oct 29 '13 at 03:13

3 Answers3

1

Consider:

Employee coolDude = new Employee("Billy Bob McCool", "123 Main Str", "867-5309");
System.out.println(coolDude);

Without the toString method you're asking about, this will print the class name and a hex number that will look like garbage to you, but it's actually the memory address to where coolDude exists in memory. With the toString method, you can actually print something useful. In this specific case, "Billy Bob McCool 123 Main Str 867-5309".

nhgrif
  • 61,578
  • 25
  • 134
  • 173
0

Method of Object class, which returns value of object.

According to Java Docs :

public String toString()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

Returns: a string representation of the object.

refer java docs here

codingenious
  • 8,385
  • 12
  • 60
  • 90
0

from java doc

/**
 * Returns a string representation of the object. In general, the
 * {@code toString} method returns a string that
 * "textually represents" this object. The result should
 * be a concise but informative representation that is easy for a
 * person to read.
 * It is recommended that all subclasses override this method.
 * <p>
 * The {@code toString} method for class {@code Object}
 * returns a string consisting of the name of the class of which the
 * object is an instance, the at-sign character `{@code @}', and
 * the unsigned hexadecimal representation of the hash code of the
 * object. In other words, this method returns a string equal to the
 * value of:
 * <blockquote>
 * <pre>
 * getClass().getName() + '@' + Integer.toHexString(hashCode())
 * </pre></blockquote>
 *
 * @return  a string representation of the object.
 */

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString%28%29

upog
  • 4,965
  • 8
  • 42
  • 81