Methods such as equal and hashcode in Java

Source: Internet
Author: User
Document directory
  • Equals
  • Hashcode

String class:

Public BooleanEquals(Object anobject) overload the equals method of the object
Compares this string to the specified object. The result is trueIf and only if the argument is not nullAnd is StringObject that represents the same sequence of characters as this object.
Public intHashcode() The hashcode method of the object is also overloaded.
Returns a hash code for this string. The hash code for StringObject is computed

 s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 

UsingintArithmetic, wheres[i]IsITh character of the string,nIs the length of the string, and^Indicates exponentiation. (The Hash Value of the empty string is zero .)

However, stringbuffer does not overload the equals and hashcode methods. Therefore, when calling these two methods, we still use the methods in the object class. Therefore, even if the content of the two stringbuffer is the same, the hashcode may also be different.

File class:

Compareto

public int compareTo(File pathname)
Compares two abstract pathnames lexicographically. The ordering defined by this method depends upon the underlying system. On Unix systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows systems it is not.
Equals
Public BooleanEquals(Object OBJ) reloads the object method.
Tests this abstract pathname for equality with the given object. Returns trueIf and only if the argument is not nullAnd is an abstract pathname that denotes the same file or directory as this abstract pathname. whether or not two abstract pathnames are equal depends upon the underlying system. on UNIX systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows systems it is not.
Hashcode
Public intHashcode() The object method is reloaded.
Computes a hash code for this abstract pathname. because priority ity of abstract pathnames is inherently system-dependent, so is the computation of Their hash codes. on UNIX systems, the hash code of an abstract pathname is equal to the exclusive OrOf the hash code of its pathname string and the decimal Value 1234321. On Microsoft Windows systems, the hash code is equal to the exclusive OrOf the hash code of its pathname string converted to lower case and the decimal Value 1234321. Locale is not taken into account on lowercasing the pathname string.

Therefore, create an object for a file and its copy. The hash values of these two objects may be different because they are related to the path, it does not mean that these two files are different (this refers to the file content). If hash software or MD5 software is used to calculate the hash values of these two files, it should be the same

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.