Can someone explain the function of a toString is and why it's necessary in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone explain the function of a toString is and why it's necessary in Java?

Java

14th Apr 2020, 10:03 PM
Zoe
Zoe - avatar
2 Answers
+ 2
The toString() in Java is used to represent objects in terms of strings. Whenever you try to print an object in Java, it will print 'className@hashCode' which is not a readable format so it is good to override the toString() in your class so that the content becomes more readable. Zoe run the code with and without the comments and you will understand. https://code.sololearn.com/c2ObrHJOrv1z/?ref=app
14th Apr 2020, 11:34 PM
Avinesh
Avinesh - avatar
+ 1
Okay that makes sense thank you Avinesh
14th Apr 2020, 11:52 PM
Zoe
Zoe - avatar