zoukankan      html  css  js  c++  java
  • String源码j简单分析

    分析:

    1、

     private final char value[];
    
    String内部由这个char数组维护String的字符。首先String类用final修饰,不可继承,其次,value[]用 fianl修饰,代表引用不可变。
    
        public String() {
            this.value = new char[0];
        }
    
    当调用无参构造方法时,将char数组初始化为char[0]。

    2、 String中的codePoint

    codePoint  举例来说: “我”->对应的codePoint 为十进制的25105->十六进制的6211->UNICODE编码表中的6211(“我”字在UNICODE编码表中对应的16进制数)

    3、   

    public byte[] getBytes(String charsetName)
                throws UnsupportedEncodingException {
            if (charsetName == null) throw new NullPointerException();
            return StringCoding.encode(charsetName, value, 0, value.length);
        }
    
    根据某编码格式编码

    4、equals方法

     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;
        }

    5、测试两个字符串区域是否相等。

        public boolean regionMatches(int toffset, String other, int ooffset,
                int len) {
            char ta[] = value;
            int to = toffset;
            char pa[] = other.value;
            int po = ooffset;
            // Note: toffset, ooffset, or len might be near -1>>>1.
            if ((ooffset < 0) || (toffset < 0)
                    || (toffset > (long)value.length - len)
                    || (ooffset > (long)other.value.length - len)) {
                return false;
            }
            while (len-- > 0) {
                if (ta[to++] != pa[po++]) {
                    return false;
                }
            }
            return true;
        }

     6、hashcode

    返回此字符串的哈希码。 String 对象的哈希码根据以下公式计算:

     s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     

    使用 int 算法,这里 s[i] 是字符串的第 i 个字符, n 是字符串的长度, ^ 表示求幂。(空字符串的哈希值为 0。)

        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;
        }

    7、截取子串,返回的是new 的String

        public String substring(int beginIndex) {
            if (beginIndex < 0) {
                throw new StringIndexOutOfBoundsException(beginIndex);
            }
            int subLen = value.length - beginIndex;
            if (subLen < 0) {
                throw new StringIndexOutOfBoundsException(subLen);
            }
            return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
        }

    8、字符串拼接,返回的时new String,所以不建议多次拼接,多次拼接请选StringBuffer

        public String concat(String str) {
            int otherLen = str.length();
            if (otherLen == 0) {
                return this;
            }
            int len = value.length;
            char buf[] = Arrays.copyOf(value, len + otherLen);
            str.getChars(buf, len);
            return new String(buf, true);
        }

    9、valueOf方法要注意,如果传进来的字符串为null,则会自动new String("null")返回,否则返回对象.toString()

        public static String valueOf(Object obj) {
            return (obj == null) ? "null" : obj.toString();
        }

    10、intern()方法分析

    返回字符串对象的规范化表示形式。

    一个初始时为空的字符串池,它由类 String 私有地维护。

    当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并且返回此 String 对象的引用。

    它遵循对于任何两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。

    所有字面值字符串和字符串赋值常量表达式都是内部的。

    返回:

    一个字符串,内容与此字符串相同,但它保证来自字符串池中。

    希望深入了解的看我的这篇文章:http://www.cnblogs.com/lige-H/p/7216544.html

    最后附上看到的其他大手写的比较有深度的分析,随时补充自己的文章~~~

    http://www.jianshu.com/p/799c4459b808

  • 相关阅读:
    Selenium 3 + BrowserMobProxy 2.1.4 模拟浏览器访问 (含趟坑)
    macOS Sierra WiFi connecting problem
    Accumulator<Long> implements of JavaSparkContext in Spark1.x
    写了一个Android动画的启动界面
    用C#简单实现了数据的封装
    关于JAVA数据结构中的栈操作
    写了一个关于将XML文件导入数据库的程序(C#,sql server)
    经典电影里的数学应用
    初步学习多线程操作,代码不是完美的,欢迎大牛指点(运行通过)
    写了一份统计网站(ASP.NET)日访问量的源码(保存至数据库,部分性能待优化),运行通过。
  • 原文地址:https://www.cnblogs.com/lige-H/p/7213995.html
Copyright © 2011-2022 走看看