码迷,mamicode.com
首页 > 编程语言 > 详细

JavaSE---String

时间:2020-03-19 10:47:59      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:ini   style   pre   encoding   throws   div   源码解读   ++   col   

 

1、概述

    1.1、Strings are constant; their values cannot be changed after they are created.(字符串 是 常量,一旦被创建 值 不能被修改);

 

2、源码解读

public final class String implements java.io.Serializable, Comparable<String>, CharSequence {

  private final char value[];
  private int hash;
  



  public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }

   public String(byte bytes[]) {
        this(bytes, 0, bytes.length);
    }


  public String(StringBuffer buffer) {
        synchronized(buffer) {
            this.value = Arrays.copyOf(buffer.getValue(), buffer.length());
        }
    }

   public String(StringBuilder builder) {
        this.value = Arrays.copyOf(builder.getValue(), builder.length());
    }

   public int length() {
        return value.length;
    }

   public char charAt(int index) {
        if ((index < 0) || (index >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
    }

   public byte[] getBytes() {
        return StringCoding.encode(value, 0, value.length);
    }

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

   public byte[] getBytes(Charset charset) {
        if (charset == null) throw new NullPointerException();
        return StringCoding.encode(charset, value, 0, value.length);
    }

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

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


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

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

   public static String valueOf(int i) {
        return Integer.toString(i);
    }

   public native String intern();

  

JavaSE---String

标签:ini   style   pre   encoding   throws   div   源码解读   ++   col   

原文地址:https://www.cnblogs.com/anpeiyong/p/12522515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!