String.equals()如何工作 [英] how does String.equals() work

查看:85
本文介绍了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而(n--!= 0){
1026 if(v1 [i ++]!= v2 [j ++])
1027 return false;
1028}
1029 return true;
1030}
1031}
1032返回false;
1033}


解决方案

逻辑

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



相同($ i $ 0; i< n; i ++){
if(v1 [i]!= v2 [j])
返回false ;
}
}

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



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

  if(v1 [i ++]!= v2 [ j ++])
返回false;

检查字符串底层字符数组中的字符。



并且逐行它是



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

  1014 if(this == anObject){
1015 return true;
1016}

如果对象是字符串然后检查它们是否相等

  1017 if(anObject instanceof String){

将传入的参数转换为String。

  1018 String anotherString =(String)anObject; 

记住this.string的长度

  1019 int n = count; 

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

  1020 if(n == anotherString.count){

get一个字符数组(值是这个数组)

  1021 char v1 [] = value; 
1022 char v2 [] = anotherString.value;

找出数组中字符串开始的位置

  1023 int i = offset; 
1024 int j = anotherString.offset;

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

  1025而(n--!= 0){
1026 if( v1 [i ++]!= v2 [j ++])
1027返回false;
1028}

其他一切必须是真的

  1029返回true; 
1030}
1031}

如果不是String类型那么它们不能是等于

  1032返回false; 
1033}

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

  / **该值用于字符存储。 * / 
私有最终char值[];

/ **偏移量是使用的存储的第一个索引。 * /
private final int offset;

/ **计数是字符串中的字符数。 * /
私人最终int计数;

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

  / ** 
*初始化新创建的{@code String } object,表示
*一个空字符序列。请注意,由于字符串是不可变的,因此使用此构造函数是
*。
* /
public String(){
this.offset = 0;
this.count = 0;
this.value = new char [0];
}

这个是一个很好的链接,可以看看


I have been trying to understand how some of API methods work

below is a snippet of equals method of java.lang.String class

Can someone out there tell me how actually the code is comparing two strings. I get the significance of count, but what does offset signify. how are these variables getting values ?

Like when i create a String. how are these initialized.

a detailed line by line description and also how and when the instance variables, value, count, offset etc are initialized ??

 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       }

解决方案

Logically

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

is the same as

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

Why the JVM designers have done it this way I am not sure. Perhaps there is a performance improvement using a while loop than a for loop. It looks quite C like to me so maybe the person who wrote this has a background in c.

Offset is used to locate where the string starts within the char array. Internally Strings are stored as char arrays. This is value

if (v1[i++] != v2[j++])
    return false;

checks the characters in the string's underlying char array.

and line by line it is

if the refernce is pointing to the same object is must the equals

1014           if (this == anObject) {
1015               return true;
1016           }

if the object is a string then check they are equal

1017           if (anObject instanceof String) {

cast the parameter passed in as String.

1018               String anotherString = (String)anObject;

remember the length of this.string

1019               int n = count;

if the two string's lengths match

1020               if (n == anotherString.count) {

get an array of the characters (value is this array)

1021                   char v1[] = value;
1022                   char v2[] = anotherString.value;

find out where in this array the string starts

1023                   int i = offset;
1024                   int j = anotherString.offset;

loop through char array. if the values are different then return false

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

everything else must be true

1029                   return true;
1030               }
1031           }

if not of type String then they cannot be equals

1032           return false;
1033       }

To understand offset and value look at the String class

/** 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;

The constructors initialises these variables. The default constructor code is below. You should see something similar for the other constructors.

/**
  * 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];
 }

This is quite a good link to look at

这篇关于String.equals()如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆