Is the toString method needed?

I recently watched a tutorial on toStrings since I still haven't used a lot of them, and it occurred to me that there might be an easier way to do the same with less code. Of course, the code below gave an accurate result with less code.

If so, what is the point of toString? Is there any advantage to using toStrings over how I 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); } } 
+4
source share
6 answers

According to Java doc toString()

Returns a string representation of an object [...]

In fact, many of the native Java methods and methods for libs call this method to get a string representation.

For example, Arrays.toString(Object[] a) calls
toString for each object in array a.
Or print methods of PrintStream , which accepts
the object as an argument calls this indirectly through Strings.valueOf(Object obj)

+2
source

toString() method will help you in many cases

Lower on my head

  • If you want to just print the object pretty nicely
  • When you are debugging, if you have toString() expressive way, you will not need to expand the instance and see its field in the debugger
+1
source

I believe that because there is an internal toString (or the equivalent, which for all practical purposes toString) is called part of the concatenation operator when you do this.

toString is still useful for more control over output formatting, etc. But if you don’t need it, that’s fine.

+1
source

The reason your example works is actually due to the utility of toString() . You combine the String ( name ) object and the int ( age ) literal with the String ( ", " ) literal in your print statement. What this does is implicitly call toString() for everything concatenated. The String object has the toString() method defined in it, which returns the contents of the String internal char array for concatenation. int just uses its integral value as a primitive (although there may be some autoboxing that happens there in Integer , but it just ends up calling the toString() method defined in the Integer class).

What you really should try is to print your actual Daughter object (I use Daughter Daughter with this standard Java practice for class names):

 public static void main(String args[]) { Daughter d = new Daughter("Elizabeth", 7); System.out.println("Daughter: " + d); } 

You will notice that your result will look something like this:

Daughter: Daughter @ a62b39f

This is because you do not have the toString() method defined in your Daughter class, so it uses the return value of the default toString() / a> method , which is defined in the java.lang.Object class ("Mother of all objects", as some people call it, since all objects in Java are ultimately inherited from java.lang.Object ). This default method, toString() simply prints the class name and the storage location in which this particular object is located (technically the hash code of the object, but this is usually just a memory address, which is usually not so useful).

If you define the toString() method in your Daughter class as follows:

 public String toString() { return this.name + ", " + this.age; } 

then you should get a conclusion like this

Daughter: Elizabeth 7

when you call System.out.println("Daughter: " + d); to its main method (or elsewhere, the toString() method is called, explicitly or implicitly, on any Daughter object).

+1
source

Using the toString method, you can print information anywhere with a child

0
source

Just look at the Date class that it uses for toString to give the user the ability to see the current Date&Time in the object:

 System.out.println(new Date()); 

prints: Mon Aug 29 13:22:03 BST 2012

in addition to it, the ability to convert objects without String (which can be converted to strings)
Example:

 Integer x=4; System.out.println(x.toString)// prints: 4 
0
source

Source: https://habr.com/ru/post/1481551/


All Articles