jdk source of String

Outline

String type is a java language most commonly used type, has many features.

  1. Not inheritance
  2. Immutability
  3. Reference types
  4. Constant pool

String string constants, is generated by the compiler, to maintain constant pool; runtime methods zone.

String a = "Hello";//常量池
String b = "World";//常量池
String c = "HelloWorld";//常量池
String d = "Hello" + "World";//常量池
String e = a + b;//常量池
String f = new String("HelloWold");//在堆中

and so

  1. c, d, e is the same address, and f , and c, d, e are different.
  2. The first is to create a literal String constant pool looking at whether there is the same, not only in the constant pool is created.
  3. native intern() String object local methods for the stack, the sequence of the pool to store constants.

Source

First, property
public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence
{
    /** 字符串数组 */
    private final char value[];

    /** 偏移量 */
    private final int offset;

    /** 字符串长度 */
    private final int count;

    /** hash值 */
    private int hash; // Default to 0
    }

1, String of storage actually maintains a Char[] Array for storing strings, are to be finalModified, so it reflects the String immutable nature.
2, String class isfinal Modification, it represents the class can not be inherited.

Common construction method

1, no reference constructor

public String() {
	this.offset = 0;
	this.count = 0;
	this.value = new char[0];
}

2、String

 public String(String original) {
	int size = original.count;
	char[] originalValue = original.value;
	char[] v;
  	if (originalValue.length > size) {
        int off = original.offset;
        v = Arrays.copyOfRange(originalValue, off, off+size);
 	} else {
	    v = originalValue;
 	}
	this.offset = 0;
	this.count = size;
	this.value = v;
    }

3, an array of characters

public String(char value[]) {
	this.offset = 0;
	this.count = value.length;
	this.value = StringValue.from(value);
}

4, specify the character code

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

5, the byte array

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

6、StringBuffer、StringBuilder

public String(StringBuffer buffer) {
        String result = buffer.toString();//调用应用类型的toString 方法
        this.value = result.value;
        this.count = result.count;
        this.offset = result.offset;
    }
 public String(StringBuilder builder) {
        String result = builder.toString();/调用应用类型的toString 方法
        this.value = result.value;
        this.count = result.count;
        this.offset = result.offset;
    }   
equals和hashCode、toString
public boolean equals(Object anObject) {
	if (this == anObject) {
	    return true;
	}
	if (anObject instanceof String) {
	    String anotherString = (String)anObject;
	    int n = count;
	    if (n == anotherString.count) {
		char v1[] = value;
		char v2[] = anotherString.value;
		int i = offset;
		int j = anotherString.offset;
		while (n-- != 0) {
		    if (v1[i++] != v2[j++])
			return false;
		}
		return true;
	    }
	}
	return false;
    }

public int hashCode() {
	int h = hash;
	if (h == 0) {
	    int off = offset;
	    char val[] = value;
	    int len = count;

            for (int i = 0; i < len; i++) {
                h = 31*h + val[off++];
            }
            hash = h;
        }
        return h;
    }
    
public String toString() {
	return this;
    }

A list of commonly used methods

1、length()

public int length() {
        return count;
    }

2、isEmpty()

public boolean isEmpty() {
	return count == 0;
    }

3、charAt(int index)

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

4、getBytes()

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

5、equalsIgnoreCase(String anotherString)

public boolean equalsIgnoreCase(String anotherString) {
        return (this == anotherString) ? true :
               (anotherString != null) && (anotherString.count == count) &&
	       regionMatches(true, 0, anotherString, 0, count);
    }

6、compareTo(String anotherString)

 public int compareTo(String anotherString) {
	int len1 = count;
	int len2 = anotherString.count;
	int n = Math.min(len1, len2);
	char v1[] = value;
	char v2[] = anotherString.value;
	int i = offset;
	int j = anotherString.offset;

	if (i == j) {
	    int k = i;
	    int lim = n + i;
	    while (k < lim) {
		char c1 = v1[k];
		char c2 = v2[k];
		if (c1 != c2) {
		    return c1 - c2;
		}
		k++;
	    }
	} else {
	    while (n-- != 0) {
		char c1 = v1[i++];
		char c2 = v2[j++];
		if (c1 != c2) {
		    return c1 - c2;
		}
	    }
	}
	return len1 - len2;
    }

7、istartsWith(String prefix)

public boolean startsWith(String prefix) {
	return startsWith(prefix, 0);
    }

8、endsWith(String suffix)

 public boolean endsWith(String suffix) {
	return startsWith(suffix, count - suffix.count);
    }

9、indexOf(String str)

public int indexOf(String str) {
	return indexOf(str, 0);
    }

10、substring(int beginIndex)

 public String substring(int beginIndex) {
	return substring(beginIndex, count);
    }

11、replace(char oldChar, char newChar)

public String replace(char oldChar, char newChar) {
	if (oldChar != newChar) {
	    int len = count;
	    int i = -1;
	    char[] val = value; /* avoid getfield opcode */
	    int off = offset;   /* avoid getfield opcode */

	    while (++i < len) {
		if (val[off + i] == oldChar) {
		    break;
		}
	    }
	    if (i < len) {
		char buf[] = new char[len];
		for (int j = 0 ; j < i ; j++) {
		    buf[j] = val[off+j];
		}
		while (i < len) {
		    char c = val[off + i];
		    buf[i] = (c == oldChar) ? newChar : c;
		    i++;
		}
		return new String(0, len, buf);
	    }
	}
	return this;
    }

12、boolean matches(String regex)

public boolean matches(String regex) {
        return Pattern.matches(regex, this);
    }

13、*** String[] split(String regex)***

public String[] split(String regex) {
        return split(regex, 0);
    }

14、String toLowerCase()

public String toLowerCase() {
        return toLowerCase(Locale.getDefault());
    }

15、String toUpperCase()

public String toUpperCase() {
        return toUpperCase(Locale.getDefault());
    }

16、String trim()

public String trim() {
	int len = count;
	int st = 0;
	int off = offset;      /* avoid getfield opcode */
	char[] val = value;    /* avoid getfield opcode */

	while ((st < len) && (val[off + st] <= ' ')) {
	    st++;
	}
	while ((st < len) && (val[off + len - 1] <= ' ')) {
	    len--;
	}
	return ((st > 0) || (len < count)) ? substring(st, len) : this;
    }

17、String format(String format, Object … args)

 public static String format(String format, Object ... args) {
	return new Formatter().format(format, args).toString();
    }

18、String valueOf(Object obj)

public static String valueOf(Object obj) {
	return (obj == null) ? "null" : obj.toString();
    }
public static String valueOf(int i) {
        return Integer.toString(i, 10);
    }    
Published 66 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/qq125281823/article/details/104950988