Comparison of Java strings CompareTo

Source: Internet
Author: User

Import Java.util.Scanner;

public class Demo01 {
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
for (;;) {
String a = Sc.next ();
String B = Sc.next ();
int num = A.compareto (b);
SYSTEM.OUT.PRINTLN (num);
}
}
}

1. When two comparison strings are in English and vary in length,

1) As long as the length of a character, the returned result is a two-length subtraction value

A= "Hello";

B= "Hell";

Num=1;

Or

A= "H";

b= "Hello";

num=4;

2) The length is different and the first few characters are different, starting from the first bit, when a different character is found, the value returned is the value of the comparison between the two characters

A= "ASSDF";

b= "BDD";

Num=-1;

2. When the two comparison strings are in English and are of equal length,

1) A single character

A= "a"; 97

B= "B"; 98

Num=-1;

2) Multiple characters, the first character is different, the first character is directly compared

A= "Ah"; a=97

b= "EG"; E=101

num=-4;

3) Multiple characters, the first character is the same, the second character is directly compared, and so on

A= "AE"; E=101

b= "AA"; a=97

num=4;

Comparison of Java strings CompareTo

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.