Meaning of the string returned by Object.ToString () in Java

Source: Internet
Author: User

ToString () is a public method of the object class, and all classes inherit from the object class. So, even if the ToString method is not implemented for all classes, there will be ToString that inherits from the object class.

Class can implement the ToString method, printing an object in the console automatically calls the ToString method of the object class, so we can implement our own ToString method to display useful information about the class in the console.

While eight basic data types do not have the ToString () method, you can use ToString () only with the appropriate wrapper class.

The string returned by the ToString method of object is much like the object name [email protected]+ object memory address, but that is not the case.

Two test codes are written here.

The first paragraph, in list, stores the string returned by the object's ToString.

In the second paragraph, the object itself is stored in the list.

Both pieces of code try to find duplicate elements in the list and print them out.

 Public Static voidList_obj_tostring (intsize) {List<Object> list =NewArraylist<>(); intRepeated = 0;  for(inti = 0; i < size; i++) {Object obj=NewObject (); if(List.contains (obj.tostring ())) {System.out.println ("Object" + obj.tostring () + "has repeated!"); Repeated++; } Else{List.add (obj.tostring ()); }} System.out.println ("Total object.tostring ():" +size); System.out.println ("Repeated object.tostring ():" +repeated); }
 Public Static voidList_obj (intsize) {List<Object> list =NewArraylist<>(); intRepeated = 0;  for(inti = 0; i < size; i++) {Object obj=NewObject (); if(List.contains (obj)) {System.out.println ("Object" + obj.tostring () + "has repeated!"); Repeated++; } Else{list.add (obj); }} System.out.println ("Total Object:" +size); System.out.println ("Repeated Object:" +repeated); }
     Public Static void Main (string[] args) {        list_obj_tostring (2000000);        List_obj (2000000);    }

Each of these methods is called in main and results are obtained

Object.ToString () [email protected] has repeated! object.tostring () [email protected] has repeated! ... ... Object.ToString () [email protected] has repeated! object.tostring () [email protected] has repeated! Total object.tostring ():200000repeated object.tostring (): 8 TotalObject:200000  Repeated Object:0

From the result, the object's ToString has been repeated 8 times, and the object's handle (another argument is "object reference"), of course, is not repeated at one time.

What's the problem? At least we can now be certain that the "object name [email protected]+ object memory Address" is definitely wrong because the memory address is definitely not duplicated.

Let's take a look at the Java API documentation, where there is a description of object.tostring ()

The ToString method for class Object returns a string consisting of the name of the class of which the object was an Instan CE, the at-sign character ' @ ', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value Of:getclass (). GetName () + ' @ ' + integer.tohexstring (HASHC Ode ())

Then, the reason is found. After the @ is the hash of the object converted to 16, so when the hash conflict, the string returned by Object.ToString () inevitably repeats.

Later someone said that ToString will print out the object's memory address, you can not hesitate to refute him.

PS: During this test, the List_obj_tostring method took about 35% of the CPU (Intel i7 6500U) in idea to run for about three or four minutes, while List_obj probably only took less than five seconds, visible, toString () The method shows its low time efficiency at 200000, presumably due to a large number of string concatenation. I hope we'll have time to do a test later.

PS: When the list_obj_tostring parameter size is 100000, 0 repetitions occur. When the parameter is 200000, 8 repetitions occur. The hash Algorithm for Hashcode () in Java 10 will begin to have a large number of hash collisions at this order of magnitude.

Meaning of the string returned by Object.ToString () in Java

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.