String API(整理中)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhoukikoo/article/details/79389403

String API

A

B

C

D

E

equals

/**
 * Compares this string to the specified object.  The result is {@code
 * true} if and only if the argument is not {@code null} and is a {@code
 * String} object that represents the same sequence of characters as this
 * object.
 * 将此字符串与指定的对象进行比较
 * 如果此参数不为null且与指定对象表示的是同一个字符串时,返回结果为true
 *
 * @param  anObject
 *         The object to compare this {@code String} against
 *         参数:要与此字符串对象进行比较的对象 
 *
 * @return  {@code true} if the given object represents a {@code String}
 *          equivalent to this string, {@code false} otherwise
 *          返回值:若给定的对象等同于此字符串,则返回true,否则返回false
 *
 * @see  #compareTo(String)
 * @see  #equalsIgnoreCase(String)
 */
public boolean equals(Object anObject) {
    // ==的作用:比较两个对象的引用,引用相同,值一定相同
    if (this == anObject) {
        return true;
    }
    // instanceof:判断其左边对象是否为其右边类的实例
    // anObject若不是String类的实例,则压根不用比了,直接false
    if (anObject instanceof String) {
        String anotherString = (String)anObject;// 若是,则先强转Object为String,再参与比较
        int n = value.length;// 指定字符串的长度
        if (n == anotherString.value.length) {// 长度都不一样,肯定值不同,直接false
            // strObj.equals(anObject) 
            char v1[] = value;// strObj的字符数组
            char v2[] = anotherString.value;// anObject的字符数组
            int i = 0;
            while (n-- != 0) {// 比较2个字符数组相同下标的元素值是否相同
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

关于 instanceofgetClass()
instanceof 判断当前左边实例是不是右边类(或右边类的派生类)的实例
getClass== 判断实例类型是否为右边类型
getClass 严格性比 instanceof 要高!

@Test
public void instanceofTest() {
    String i = "xws";
    System.out.println(i instanceof String);// true
    System.out.println(i instanceof Object);// true
}

@Test
public void getClassTest() {
    Parent parent = new Parent();
    System.out.println(parent.getClass() == Child.class);// false
    System.out.println(parent.getClass() == Parent.class);// true
}

class Child extends Parent {
}

class Parent {
}

F

G

H

/**
 * Returns a hash code for this string. The hash code for a
 * {@code String} object is computed as
 * <blockquote><pre>
 * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
 * </pre></blockquote>
 * using {@code int} arithmetic, where {@code s[i]} is the
 * <i>i</i>th character of the string, {@code n} is the length of
 * the string, and {@code ^} indicates exponentiation.
 * (The hash value of the empty string is zero.)
 *
 * @return  a hash code value for this object.
 */
public int hashCode() {
    int h = hash;
    if (h == 0 && value.length > 0) {
        char val[] = value;

        for (int i = 0; i < value.length; i++) {
            h = 31 * h + val[i];
        }
        hash = h;
    }
    return h;
}

I

J

K

L

M

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

猜你喜欢

转载自blog.csdn.net/zhoukikoo/article/details/79389403
今日推荐