zoukankan      html  css  js  c++  java
  • equal与==

    首先做的是比较引用,引用的如果是同一个对象,直接返回true。
    做完return就结束了。
    如果引用不是同一个地址,就往下走,判断是否是String的一个实例。同样,不是的话直接返回。
    是的话,拿字符串的长度做循环的控制变量,做循环。此处的value在源代码里面来看,应该就是String的混:字符数组。
    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;
        }

  • 相关阅读:
    Odd sum CodeForces
    Chips CodeForces
    Secrets CodeForces
    Voting CodeForces
    Jury Meeting CodeForces
    Planning CodeForces
    Maxim Buys an Apartment CodeForces
    Chemistry in Berland CodeForces
    Monitor CodeForces
    Four Segments CodeForces
  • 原文地址:https://www.cnblogs.com/lovehappy/p/4410021.html
Copyright © 2011-2022 走看看