Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java比较到分步执行_Java_String - Fatal编程技术网

Java比较到分步执行

Java比较到分步执行,java,string,Java,String,我基本了解JavacompareTomethodlexicalography比较两个字符串。 我在这里读过基础知识 我有以下例子: public class CompareTo { public static void main(String args[]) { String str1 = "String"; String str2 = "compareTo"; String str3 = "String"; int var1 = s

我基本了解Java
compareTo
method
lexicalography比较两个字符串。
我在这里读过基础知识

我有以下例子:

public class CompareTo {
   public static void main(String args[]) {
       String str1 = "String";
       String str2 = "compareTo";
       String str3 = "String";
       int var1 = str1.compareTo( str2 );
       System.out.println("str1 & str2 comparison: "+var1);

       int var2 = str1.compareTo( str3 );
       System.out.println("str1 & str3 comparison: "+var2);
   }
}
我得到
var1=-16
var2=0

如果有人一步一步地向我解释这个
词典比较
,那将是非常有帮助的

谢谢。

如果您打印:

System.out.println ('S'-'c');
您将获得
-16

String
compare to
将两个
字符串一次比较一个字符。第一对不相等的字符(在您的例子中是“String”的“S”和“compareTo”的“c”)决定了结果。由于按字典法,“S”在“c”之前,比较返回一个负值,这意味着“String”应该在“compareTo”之前

在第二次比较中,所有字符对都相等,因此
compareTo
从Java文档返回0。

public int compareTo(String anotherString)
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

this.charAt(k)-anotherString.charAt(k)

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:

this.length()-anotherString.length()
public int compareTo(字符串另一个字符串)
按字典顺序比较两个字符串。比较基于字符串中每个字符的Unicode值。此字符串对象表示的字符序列按字典顺序与参数字符串表示的字符序列进行比较。如果此字符串对象按字典顺序位于参数字符串之前,则结果为负整数。如果此字符串对象按字典顺序跟随参数字符串,则结果为正整数。如果字符串相等,则结果为零;当equals(Object)方法将返回true时,compareTo将返回0。
这是词典排序的定义。如果两个字符串不同,则要么它们在某个索引处具有不同的字符,该索引是两个字符串的有效索引,要么它们的长度不同,要么两者都不同。如果它们在一个或多个索引位置具有不同的字符,则k是此类索引的最小值;然后,使用<运算符确定其在位置k处的字符具有较小值的字符串按字典顺序位于另一个字符串之前。在本例中,compareTo返回两个字符串中位置k处两个字符值的差值,即:
this.charAt(k)-另一个string.charAt(k)
如果索引位置不存在差异,则按字典顺序,较短字符串优先于较长字符串。在本例中,compareTo返回字符串长度的差值,即值:
this.length()-anotherString.length()
在ASCII表中,c=99(十进制)和S=83(十进制)


为此:S-c=-16

为什么不在OpenJDK中找到Java源代码呢?可能是重复的,请尝试比较单个字符串。它将帮助您理解它。可能的副本,所以它只在这种情况下比较第一个字母,如果它是相同的,那么它将转到第二个字母。是这样吗@如果我比较“String”和“Strings”,它为什么会给出-1@Eran@rdj7如果两个字符串的长度不同,且较短的字符串完全包含在较长字符串的开头,则返回两个字符串的长度差(-1),因此较短的字符串优先。