String.equals()是如何工作的?

3
我一直在尝试理解一些API方法的工作原理。
下面是java.lang.String类中equals方法的片段。
有人能告诉我代码实际上是如何比较两个字符串的吗?我理解count的重要性,但是offset代表什么意思呢?这些变量是如何获得值的?
比如当我创建一个String时,这些变量会如何初始化?
详细的逐行描述以及实例变量value、count、offset等何时以及如何初始化?
 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       }
2个回答

9

逻辑上

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

是相同的

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

我不确定JVM设计者为什么要这样做。也许使用while循环比for循环有性能提升。在我看来,它看起来很像C语言,所以可能编写此代码的人具有C语言背景。
“Offset”用于定位字符串在char数组中的起始位置。内部字符串存储为char数组。这是“value”。
if (v1[i++] != v2[j++])
    return false;

检查字符串底层字符数组中的字符。
逐行检查。
如果引用指向同一对象,则必须使用equals。
1014           if (this == anObject) {
1015               return true;
1016           }

如果对象是字符串,则检查它们是否相等。
1017           if (anObject instanceof String) {

将传递的参数转换为字符串。
1018               String anotherString = (String)anObject;

记住 this.string 的长度。
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;

循环遍历字符数组。如果值不同,则返回 false。
1025                   while (n-- != 0) {
1026                       if (v1[i++] != v2[j++])
1027                           return false;
1028                   }

其他所有内容必须是真实的

1029                   return true;
1030               }
1031           }

如果不是字符串类型,则它们不能相等。
1032           return false;
1033       }

要理解偏移量和值,请查看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;

构造函数初始化这些变量。默认构造函数代码如下所示。您应该看到其他构造函数类似的内容。
/**
  * 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];
 }

这个链接非常不错,值得一看


这就是如此干净利落..谢谢。 - Mukul Goel
还有一个问题,我找不到一个初始化这些实例变量的构造函数。有吗?这是由JVM处理的吗? - Mukul Goel
刚刚更新了答案,包括字符串的默认构造函数,在其中进行了初始化。 - RNJ
谢谢,伙计。非常好的答案。 - Mukul Goel

0

你可能知道,在Java中处理字符串是一个特殊的情况,大多数情况下,字符串是从字符串池中分配的,因此对于一个字符数组"I am Learning Java",一个字符串引用可能指向"I am Learning Java",那么偏移量将为0,另一个字符串可能指向"am",所以偏移量将为2由于一些本地代码处理其初始化,因此我认为偏移量是在该过程中设置的(在从字符串池共享内存期间)。

此外,正如您从代码中看到的那样

 public String(String original) {
         int size = original.count;
        char[] originalValue = original.value;
        char[] v;
        if (originalValue.length > size) {
            // The array representing the String is bigger than the new
          // String itself.  Perhaps this constructor is being called
            // in order to trim the baggage, so make a copy of the array.
           int off = original.offset;
          v = Arrays.copyOfRange(originalValue, off, off+size);
        } else {
           // The array representing the String is the same
          // size as the String, so no point in making a copy.
            v = originalValue;
       }
      this.offset = 0;
       this.count = size;
       this.value = v;
    }

当从旧字符串创建新字符串时,可能会出现旧字符串(在这种情况下是原始字符串)来自字符串池的情况,因此首先需要获取偏移量,然后将整个数组复制到分配的新内存中(新字符串不共享字符串池的内存)。
另外,您应该记住字符串是一种派生类型,字符串始终存储在字符数组中,因此我们需要一个偏移量来确定字符串在字符数组中的起始位置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接