Why is the toString() method being called when I print an object?

前端 未结 5 560
北海茫月
北海茫月 2020-11-27 06:36

I can\'t seem to understand why when I use println method on the quarter object, it returns the value of the toString method. I never called the toString method why am I g

5条回答
  •  生来不讨喜
    2020-11-27 07:12

    Because this is how this function operates: it formats the primitive types for you, but when you pass it an object, it will call .toString() on it.

    If you don't override it, it will output the default .toString() implementation (Class@somenumber) which is not really useful...

提交回复
热议问题