Why does the string data type have a .ToString()
method?
The java toString() method is used when we need a string representation of an object. It is defined in Object class. This method can be overridden to customize the String representation of the Object .
String toString() is the built-in method of java. lang which return itself a string. So here no actual conversion is performed. 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.
toString() method returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read.
The toString() method This method is used to transform an object into a String. Typically, it is used to construct a string containing the information on an object that can be printed, and we can redefine it for a certain class.
The type System.String
, like almost all types in .NET, derives from System.Object
. Object
has a ToString()
method and so String
inherits this method. It is a virtual method and String
overrides it to return a reference to itself rather than using the default implementation which is to return the name of the type.
From Reflector, this is the implementation of ToString in Object
:
public virtual string ToString() { return this.GetType().ToString(); }
And this is the override in String
:
public override string ToString() { return this; }
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