To output an object, the toString() method will be executed by default

Today I found this little problem when I saw the enum knowledge point when I was looking at programming ideas (maybe my foundation is too poor)

As shown

Then I followed the source code step by step and found the mystery. First, enter the println() method as shown below

Look at the picture and execute the valueOf() method for string conversion, and then look at the valueOf source code

As shown in the figure, when the object exists, the toString() method of Object will be executed by default.

So the result is clear! When outputting an object, the toString method is executed by default. (over) 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324460010&siteId=291194637