The use of CompareTo in Java.

Source: Internet
Author: User
the CompareTo method in Java, returns the difference between the ASC codes of the previous two strings participating in the comparison, and the following set of code String a= "A", b= "B"; System.out.println (A.COMPARETO.B); the output is 1; if a= "a", b= "a" outputs 0; if a= "B", b= "a" is output 1; a single character is so compared, what if the string is longer?? If a= "AB", b= "B", then output-1, if a= "abcdef", b= "B" is output-1; that is , if the first letter of two string is different, then the method returns the first letter of the ASC code difference , if the first letter is the same?? If a= "AB", b= "a", Output 1, if a= "abcdef", b= "a" output 5, if a= "abcdef", b= "abc" Output 3, if a= "abcdef", b= "Ace" output-1; the two strings participating in the comparison if the first character is the same, Compares the next character until it is different, returns the ASC code difference of the different character, if the two string is not the same length, and the character that can participate in the comparison is exactly the same, the length difference of the two string is returned.

The use of CompareTo 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.