An object ToString () method, if not overridden, calls the ToString () method of its parent class object by default, and the ToString () method of object is the hashcode of the object, and the general hashcode is the memory address of the object.

Source: Internet
Author: User

yesterday because to get the password from the JFrame control, notice a problem, that is, the ToString method is not necessarily what you want, as follows:JPasswordField is the password input box in JFrame, if the following method is not available for the value of the password:
Jpasswordfield.getpassword (). toString ();
This is because Jpasswordfield.getpassword () gets the character array char[], and then calls the ToString method to get the hashcode of the character array, the memory address of the character array. This char[] object can only be converted to a String object using the following method.
New
What is this for? Let's look at the following example:
String s= "ABCD"; char[] C=s.tochararray (); String S1=new string (c),//The string object is constructed from a character array and assigned to S1string s2=c.tostring ();//character array calls the ToString () method, and the returned result is assigned to S2
The value of S1, S2 is not the same, the value of S1 is "ABCD", although the value of S2 is also a string, but it is only the hashcode of object C. Because the Tosting method returns the hashcode of the current object by default, the memory address.

Summary: If an object ToString () method is not overridden, the ToString () method of its parent class object is called by default, and the ToString () method of object is to print the object's hashcode, The general hashcode is the memory address of this object!

 give me another chestnut:
Integer a=10; String s=a.tostring ();
The value of S is 10 because the integer overrides the ToString () method.  enclose the JDK in the integer class to the ToString rewrite source:
     PublicString toString () {returntoString (value); }       Public StaticString toString (inti) {if(i = =integer.min_value)return"-2147483648"; intSize = (I < 0)? Stringsize (-i) + 1: Stringsize (i); Char[] buf =New Char[size];        GetChars (i, size, buf); return NewString (BUF,true); }  

An object ToString () method, if not overridden, calls the ToString () method of its parent class object by default, and the ToString () method of object is the hashcode of the object, and the general hashcode is the memory address of the object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.