The compareTo() method is used to compare the Number object with the parameter of the method

The compareTo() method is used to compare the Number object with the method's parameters. Can be used to compare Byte, Long, Integer, etc. This method is used to compare two same data types, and two different types of data cannot be compared by this method.

grammar

public int compareTo( NumberSubClass referenceName )

parameter

referenceName – can be a parameter of type Byte, Double, Integer, Float, Long or Short.

return value

1. Return 0 if the specified number is equal to the parameter.

2. If the specified number is less than the parameter, return -1.

3. If the specified number is greater than the parameter, return 1.

insert image description here

The compareTo() function usage in Java compares another function character: public int compareTo(String anotherString)

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the string. Compares the character sequence represented by this String object with the character sequence represented by the argument string. If this String object lexicographically precedes the argument string, the result of the comparison is a negative integer.

If this String object lexicographically follows the argument string, the result of the comparison is a positive integer. The result is 0 if the two strings are equal; compareTo returns 0 only if the method equals(Object) returns true.
This is the definition of lexicographical ordering. If the two strings are different, either they have different characters at an index that is a valid index for both, or they have different lengths, or both.

If they have different characters at one or more index positions, let k be the smallest value of such indices; then the string determined by the < operator has the smaller value at position k, whose lexicographical order is in other before the string.

In this case, compareTo returns the two different char values ​​of the two strings at position k, namely the value:
this.charAt(k)-anotherString.charAt(k)
If they do not have different index positions, compare Short strings precede longer strings lexicographically. In this case, compareTo returns the difference in the lengths of the two strings, ie the value:
this.length()-anotherString.length()
Specified by: compareTo Parameters in interface Comparable
: anotherString - the String to compare.

Returns: a value of 0 if the argument string is equal to this string, a value less than 0 if this string is lexicographically less than the string argument, or a value less than 0 if this string is lexicographically greater than the string argument A value greater than 0.

Extended information:

compareTo is to compare two values. If the former is greater than the latter, it returns 1, if it is equal, it returns 0, and if it is less than, it returns -1. I give an example below. Since the variable I am comparing is int, the int type can be directly compared, so there is no Use compareTo comparison, if the declaration is Date, String, Integer or others, you can directly use compareTo comparison, such as the following function code usage:

public int compareTo(TestModel1 o) {

return this.str1.compareTo(o.str1);

}

1. When the two strings to be compared are in English and have different lengths,

1) If the character with short length is the same as the character with long length, the returned result is the value of the subtraction of the two lengths

a=“hello”;

b=“hell”;

num=1;

or

a=“h”;

b=“hello”;

num=4;

2) The length is different and the first few characters are also different, start searching from the first character, when a different character is found, the returned value is the value compared with the two characters

a=“assdf”;

b=“bdd”;

num=-1;

2. When the two strings to be compared are in English and have the same length,

1) a character

a=“a”; //97

b=“b”; //98

num=-1;

2) Multiple characters, if the first character is different, compare the first character directly

a="ah"; //a=97

b=“eg”; //e=101

num=-4

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

a=“yes”; //e=101

b=“aa”; //a=97

num=4;

Guess you like

Origin blog.csdn.net/qq_39236283/article/details/125260747