java - Is the toString Method Unnecessary? -


i watching tutorial on tostrings recently, since still haven't used them much, , occurred me there might easier way of accomplishing same thing less code. surely enough, code below produced exact same result less code.

if case, point of tostring? there advantage using tostrings on way did it?

public class daughter{     private static string name;     private static int age;      public daughter(string name, int age){         this.name = name;         this.age = age;          system.out.println(name + ", " + age);     }      public static void main(string args[]){         new daughter("elizabeth", 7);     } } 

according java doc tostring()

returns string representation of object[...]

in fact, many native java methods , methods of libs
call method string representation.

for example arrays.tostring(object[] a) calls
tostring every object in array a.
or print methods of printstream takes
object argument calls indirectly through
strings.valueof(object obj)


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -