Thinking: toString ()

xiaoxiao2021-03-06  120

Thinking In Java Second Edition 166 pages about Array initialization code: Class a {INT I;}

Public class varargs {static void f (Object [] x) {for (int i = 0; i

Run results: 47varargs @ 1ba34f23.1411.11netwothreea @ 13f5d07a @ f4a24aa @ cac268

================

I understand this code:

Java creates references to this object using new objectName (). Each object inherits a toString () method from Object. When using System.out.Println output a reference, the TSTRING () method of the object is actually called. For classes without the Tostring method, PrintLn outputs "address" of this instance.

When printing, if you don't have to overload the toString () method in your code, Java will automatically call the Object's toString () method, and Object's toString () method is usually printing an address of an object, so Here Class A, because you do not have an overload toString () method, so call the toString () method of Object, print the address. For some classes, Double objects, such as here, because Java has been overloaded to Tostring (), although you have not reloaded again, but it still prints according to the toString () method already existing in Double.

DanceFlash Different understandings of output addresses in this code

About call the toString () method of object () This completely agrees

However, it should be noted that the printed "A @ 13f5d07" is not his "address", but the class of the object and the hash value of the object.

The following is the code of the toString () method of the Object class: public string toString () {return getclass (). Getname () "@" integer.tohexstring (hashcode ());}

Where: getClass (). GetName () Returns the class name "of the object" Hashcode () Returns the hash value of the object Integer.toHexString (hashcode ()) expands the hash value of the object

So finally got this result: a @ 13f5d07

==============================================================================================================================================================================================================================================

Public class b {public string toString () {return "this is a";}} public class a {public static void main (string [] args) {system.out.println (new b ());}}

Output: this is a

Understand: Because the TOString method has been declared in B, it is actually overloaded to the toString method. At this time, system.out.println (Object) automatically calls the Object's toString method output.

Any Class in Java is extension from Object, there is a toString () method, system.out.println (object) will call this object's toString method by default. If you rewrive this toString in Class, rewriting this toString () Method, that is, when the output is output, the called Tositring method is defined in the class.

In fact, the parameter of System.out.Println () is String. He will convert the parameters to the String type, to rewrite the toString method, so when the new b () is converted into string, the toString function will be called. The called is the function you define.

Note: Because what to write, what is more chaotic, forgive me. . ^ _ ^

转载请注明原文地址:https://www.9cbs.com/read-126615.html

New Post(0)