For example, let's say I am adding a set of objects I created, called myClass
(which contains a string) to myList
and then I wish to use the list and pass it to a GUI list. Is there a way to populate that GUI list only with the value of that string in myClass
?
What is toString()? A toString() is an in-built method in Java that returns the value given to it in string format. Hence, any object that this method is applied on, will then be returned as a string object.
The toString method is implemented by default in the class Object (docs.oracle.com/javase/7/docs/api/java/lang/…). This method is not abstract so you don't have to override it. You can if you want to change the string representation of you object.
In Python, the equivalent of the tostring() is the str() function. The str() is a built-in function. It can convert an object of a different type to a string. When we call this function, it calls the __str__() function internally to get the representation of the object as a string.
JavaScript calls the toString method automatically when a Function is to be represented as a text value, e.g. when a function is concatenated with a string.
Every object should respond to printString
message with some meaningful string presentation of itself. You can write your own #printString for your object and use it.
Even better for your case could be implementation of asString
conversion method, which converts your object to a string.
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