ToString method of object in Java

CSDN Q & A 2022-02-13 05:10:45 阅读数:139

tostring method object java

example :Object a="hello";
The result is :hello

Print Object Automatically called toString, Shouldn't hash code values be printed ?( Or Xiaobai , It's not very clear here )

Take the answer :

Very simple! . because a="hello" Is a constant . And for string type, it is a special reference type , When outputting a variable of type string , All output content , Not the address .
About System.out.println(str) What is printed is a string value , Not the address :
If help , Please accept . Click on the top right corner of my answer 【 Adopt 】 Button .

Other answers 2:

JAVA Program print objects are automatically called toString Method , So we often have to rewrite the of objects toString Method .

Other answers 3:

java Dynamic binding principle of , The called is the of the current object toString() Method , The print result is no longer hashid
Object Class hashCode. Returns the processed structure of the memory address of the object , Because the memory address of each object is different , So hash codes are different .

copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you.