Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toString: When is it used?

Tags:

java

tostring

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 perfectly fine, I use it all the time. Now when I do this:

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

I get the 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 has to concatenate two Strings, but the second one is a Configuration. So Configuration#toString() is called and everything is fine. In the compiler error case the compiler sees that a String is needed, but a Configuration is given. Basically the same problem.

  • Needed: String
  • Given: Configuration

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

like image 670
brimborium Avatar asked Jul 30 '12 08:07

brimborium


People also ask

Why is toString needed?

In most languages, toString or the equivalent method just guarantees that an object can be represented textually. This is especially useful for logging, debugging, or any other circumstance where you need to be able to render any and every object you encounter as a string.

What is the use of toString () method in Java?

Java - toString() Method The method is used to get a String object representing the value of the Number Object. If the method takes a primitive data type as an argument, then the String object representing the primitive data type value is returned.

Is toString required for all classes?

You can if you want to change the string representation of you object. The point in having a toString method in your abstract Animal class was probably exactly this: then you don't need to declare one in the child classes (or at least in some of your child classes you don't need).

Does Java automatically use toString?

Every object in Java IS-A(n) Object as well. Hence, if a toString() implementation has not been provided by a class the default Object. toString() gets invoked automatically.

What does the toString () method do in Java?

The toString () method returns a string as a string. The toString () method does not change the original string. The toString () method can be used to convert a string object into a string.

Why we can't use ToString () method to print the string value?

We have not used toString () to print the String value, because println () method by default uses toString () method internally. We have not implemented the toString () method, Java has itself overridden it for us.

What is the return value of toString?

Since toString () method simply returns the current string without any changes, there is no need to call the string explicitly, it is usually called implicitly. Parameter: The method does not accept any parameters . Return Value: This method returns the string itself.

What is the difference between tostring () and I in C++?

i − An int for which string representation would be returned. toString () − This returns a String object representing the value of this Integer. toString (int i) − This returns a String object representing the specified integer.


1 Answers

While designing the language, someone decided that when a programmer appends an arbitrary object to a string using the + operator, they definitely want a String, so implicitly calling toString() makes sense.

But if you call an arbitrary method that takes a String with something else, that is simply a type error, exactly what all that static typing is supposed to prevent.

like image 101
Michael Borgwardt Avatar answered Nov 08 '22 12:11

Michael Borgwardt