String.equals()如何工作

Muk*_*oel 2 java string equals

我一直试图了解一些API方法的工作原理

下面是java.lang.String类的equals方法的片段

有人可以告诉我代码实际上是如何比较两个字符串的.我得到了计数的重要性,但偏移意味着什么.这些变量如何获得价值?

就像我创建一个String.这些是如何初始化的.

详细的逐行描述以及实例变量,值,计数,偏移等的初始化方式和时间?

 public boolean equals(Object anObject) {
  1014           if (this == anObject) {
  1015               return true;
  1016           }
  1017           if (anObject instanceof String) {
  1018               String anotherString = (String)anObject;
  1019               int n = count;
  1020               if (n == anotherString.count) {
  1021                   char v1[] = value;
  1022                   char v2[] = anotherString.value;
  1023                   int i = offset;
  1024                   int j = anotherString.offset;
  1025                   while (n-- != 0) {
  1026                       if (v1[i++] != v2[j++])
  1027                           return false;
  1028                   }
  1029                   return true;
  1030               }
  1031           }
  1032           return false;
  1033       }
Run Code Online (Sandbox Code Playgroud)

RNJ*_*RNJ 9

按道理

while (n-- != 0) {
if (v1[i++] != v2[j++])
    return false;
}
Run Code Online (Sandbox Code Playgroud)

是相同的

for (int i = 0; i < n; i++) {
    if (v1[i] != v2[j])
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么JVM设计师这样做我不确定.也许使用while循环而不是for循环可以提高性能.它看起来很像C,所以写这个的人可能有c的背景.

Offset用于定位字符串在char数组中的开始位置.内部字符串存储为char数组.这是value

if (v1[i++] != v2[j++])
    return false;
Run Code Online (Sandbox Code Playgroud)

检查字符串的基础char数组中的字符.

它是逐行的

如果引用指向同一个对象则必须等于

1014           if (this == anObject) {
1015               return true;
1016           }
Run Code Online (Sandbox Code Playgroud)

如果对象是一个字符串,那么检查它们是否相等

1017           if (anObject instanceof String) {
Run Code Online (Sandbox Code Playgroud)

将传入的参数转换为String.

1018               String anotherString = (String)anObject;
Run Code Online (Sandbox Code Playgroud)

记住this.string的长度

1019               int n = count;
Run Code Online (Sandbox Code Playgroud)

如果两个字符串的长度匹配

1020               if (n == anotherString.count) {
Run Code Online (Sandbox Code Playgroud)

获取一个字符数组(值是这个数组)

1021                   char v1[] = value;
1022                   char v2[] = anotherString.value;
Run Code Online (Sandbox Code Playgroud)

找出这个数组中字符串的起始位置

1023                   int i = offset;
1024                   int j = anotherString.offset;
Run Code Online (Sandbox Code Playgroud)

循环通过char数组.如果值不同则返回false

1025                   while (n-- != 0) {
1026                       if (v1[i++] != v2[j++])
1027                           return false;
1028                   }
Run Code Online (Sandbox Code Playgroud)

其他一切都必须是真的

1029                   return true;
1030               }
1031           }
Run Code Online (Sandbox Code Playgroud)

如果不是String类型,则它们不能等于

1032           return false;
1033       }
Run Code Online (Sandbox Code Playgroud)

要了解偏移量和值,请查看String类

/** The value is used for character storage. */
private final char value[];

/** The offset is the first index of the storage that is used. */
private final int offset;

/** The count is the number of characters in the String. */
private final int count;
Run Code Online (Sandbox Code Playgroud)

构造函数初始化这些变量.默认构造函数代码如下.您应该看到其他构造函数类似的东西.

/**
  * Initializes a newly created {@code String} object so that it represents
  * an empty character sequence.  Note that use of this constructor is
  * unnecessary since Strings are immutable.
  */
 public String() {
    this.offset = 0;
    this.count = 0;
    this.value = new char[0];
 }
Run Code Online (Sandbox Code Playgroud)

是一个很好的链接