ToString: When is it used?

I have a class

class Configuration { // various stuff @Override public String toString() { // assemble outString return outString; } } 

I also have another class

 class Log { public static void d(String format, Object... d) { // print the format using d } } 

The Log class works fine, I use it all the time. Now when I do this:

 Configuration config = getConfiguration(); Log.d(config); 

I get a compiler error The method d(String, Object...) in the type Log is not applicable for the arguments (Configuration) . I can solve this:

 Log.d("" + config); // solution 1 Log.d(config.toString()); // solution 2 

My problem: how is this different? In the first solution, the compiler notices that it must concatenate two lines, and the second - the configuration. So, Configuration#toString() is called, and everything is in order. In the event of a compiler error, the compiler sees that a string is required, but Configuration is provided. Basically the same problem.

  • Need: String
  • Provision: Configuration

How are these cases different and why is toString not called?

+6
source share
6 answers

When developing a language, someone decided that when a programmer adds an arbitrary object to a string using the + operator, they definitely need a String , so the implied call toString() makes sense.

But if you call an arbitrary method that takes a String with something else, it's just a type error, exactly what should prevent the prevention of this static typing.

+11
source

Line

 "" + config 

converted to something like

 StringBuilder sb = new StringBuilder(""); sb.append(config); 

where the second line calls

 StringBuilder.append(Object obj); 

This method calls obj.toString() to get a string representation of the object.

On the other hand, the first Log.d parameter must be String, and Java will not automatically call toString() to give the entire String value in this case.

+3
source

In one case, you pass an object argument to an operator that expects objects.

In an earlier case, you pass an object argument to a function that expects a string.

Basically the signature of the function / operator is different.

It is almost by accident [in the context of this question] that a .tostring is called when + is applied. He takes an object and does something.

As far as you know, you can pass an object if a string is mistakenly required. Therefore, it cannot blindly do .tostring ()

+2
source

One common use of toString() is the print() and println() methods of the PrintStream , as in:

 System.out.print(object); System.out.println(object); 

Basically, these two methods will call toString() on the passed object. This is one of the benefits of Polymorphism .

+2
source

Good question...

But, the Compiler does not call a method to match the formal parameters. he is just trying to use objects if possible.

But when you use the + operator, the compiler, by default, uses the toString () method of its arguments (if they are objects).

+2
source

You are passing Configuration class object argument in case 1 but in the case 2 , you are passing string argument . so no error occures You are passing Configuration class object argument in case 1 but in the case 2 , you are passing string argument . so no error occures .

+1
source

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


All Articles