Describes in detail the differences between Java and JavaScript in determining whether two strings are equal

Source: Internet
Author: User
This article mainly introduces the differences between Java and JavaScript in determining whether the two strings are equal, which is very good and has reference value, for more information about how to determine whether the two strings are the same in Java and JavaScript, see this article.

Javascript is a common scripting language, which also determines that it is not very standard relative to other programming languages. In Javascript, determine whether the two strings are equal.

Use = directly, which is the same as the String class in C ++. in Java, the equal sign is used to determine whether the references of the two strings are the same, and the equals () method or

CompareTo () method. here we need to emphasize the parameter type of the equals () method. The parameter type is definitely not a String class, but an Object class.

Some tutorials in China are written in the String class (o (character □character) o)

Let's take a look at the JDK source code:

public boolean equals(Object anObject) {    if (this == anObject) {      return true;    }    if (anObject instanceof String) {      String anotherString = (String) anObject;      int n = value.length;      if (n == anotherString.value.length) {        char v1[] = value;        char v2[] = anotherString.value;        int i = 0;        while (n-- != 0) {          if (v1[i] != v2[i])              return false;          i++;        }        return true;      }    }    return false;  }

We can see that the parameter type is the Object class. This Code is also introduced. First, we can determine whether the two references are the same. If the references are the same, the entities are naturally the same. Next we will refer to the conversion of classes:

We assign the object created by the subclass to the parent class, which is called the transformation object. On this basis, the parent class object can be converted to a subclass object. In short, the conversion between classes has certain conditions and must be determined by instanceof.

The equals () method in each class comes from the Object class, which makes it hard to understand that the parameter type of the equals () method is the Object class. It is worth mentioning that compareTo () of the String class in Java ()

Method:

 public int compareTo(String anotherString) {    int len1 = value.length;    int len2 = anotherString.value.length;    int lim = Math.min(len1, len2);    char v1[] = value;    char v2[] = anotherString.value;    int k = 0;    while (k < lim) {      char c1 = v1[k];      char c2 = v2[k];      if (c1 != c2) {        return c1 - c2;      }      k++;    }    return len1 - len2;  }

The parameter in compareTo () is a String class, because the String class implements the Comparable interface. most classes basically implement this interface (ps: one comes from inheritance, and the other comes from interfaces. This is why the two parameter types are inconsistent ).

The above is a detailed description of the differences between Java and JavaScript in determining whether the two strings are equal. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.