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.
How are these cases different and why is toString
not called?
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.
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.
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).
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With