Javaソース分析

7109 ワード

Javaソース分析
  • Javaソース分析
  • String

  • Javaソース分析
    ソースコードを学び、末端からJavaを知る.
    String
    public final class implements java.io.Serializable, Comparable, CharSequence
    

    Stringクラスはfinalクラスで継承できず、シーケンス化、コンパレータ、文字シーケンスインタフェースを実現しています.
    private static final long serialVersionUID = -6849794470754667710L;
    private static final ObjectStreamField[] serialPersistentFields =
        new ObjectStreamField[0];
    public static final Comparator CASE_INSENSITIVE_ORDER
                                         = new CaseInsensitiveComparator();
    private static class CaseInsensitiveComparator
            implements Comparator, java.io.Serializable {
    	private static final long serialVersionUID = 8575799808933029326L;
    	public int compare(String s1, String s2)
    	private Object readResolve()
    }
    

    静的変数はシーケンス値、比較器です.
    private final char value[];
    private int hash;
    

    メンバー変数はString値を格納するvalue文字配列とhash値です.
    public String()
    public String(String original)
    public String(char value[])
    public String(char value[], int offset, int count)
    public String(int[] codePoints, int offset, int count)//Unicode
    public String(byte bytes[], int offset, int length, String charsetName)//         
    public String(byte bytes[], int offset, int length, Charset charset)//         
    public String(byte bytes[], String charsetName)
    public String(byte bytes[], Charset charset)
    public String(byte bytes[], int offset, int length)
    public String(byte bytes[])
    public String(StringBuffer buffer)//  
    public String(StringBuilder builder)
    String(char[] value, boolean share)
    

    コンストラクション関数には、文字配列、バイトコード配列、StringBuffer、StringBuilderがString文字列オブジェクトに変換されます.
    private static void checkBounds(byte[] bytes, int offset, int length)//        StringIndexOutOfBoundsException
    static int indexOf(char[] source, int sourceOffset, int sourceCount,
            String target, int fromIndex)
    static int indexOf(char[] source, int sourceOffset, int sourceCount,
            char[] target, int targetOffset, int targetCount,
            int fromIndex)
    static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
            String target, int fromIndex)
    static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
            char[] target, int targetOffset, int targetCount,
            int fromIndex)
    public static String format(String format, Object... args)
    public static String format(Locale l, String format, Object... args)
    public static String valueOf(Object obj)
    public static String valueOf(char data[])
    public static String valueOf(char data[], int offset, int count)
    public static String copyValueOf(char data[], int offset, int count)
    public static String copyValueOf(char data[])
    public static String valueOf(boolean b)
    public static String valueOf(char c)
    public static String valueOf(int i)
    public static String valueOf(long l)
    public static String valueOf(float f)
    public static String valueOf(double d)
    

    静的関数には、一致を返す最初のインデックス、最後のインデックス、フォーマット、文字列の表示が含まれます.
  • フォーマット操作:
    String s = String.format("Hello %s,Welcome to my blog!", "jerry");
    System.out.println(s);//Hello jerry,Welcome to my blog!
    
  • valueOfとCopyValueOfの機能と実装はまったく同じです.
  • public int length()
    public boolean isEmpty()
    public char charAt(int index)
    public int codePointAt(int index)//unicode
    public int codePointBefore(int index)
    public int codePointCount(int beginIndex, int endIndex)
    public int offsetByCodePoints(int index, int codePointOffset)
    void getChars(char dst[], int dstBegin)//       dstBegin         
    public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)//       dstBegin         
    public byte[] getBytes(String charsetName)//  
    public byte[] getBytes(Charset charset)//  
    public byte[] getBytes()//  
    public boolean equals(Object anObject)
    private boolean nonSyncContentEquals(AbstractStringBuilder sb)
    public boolean contentEquals(CharSequence cs)
    public boolean equalsIgnoreCase(String anotherString)//      
    public int compareTo(String anotherString)
    public int compareToIgnoreCase(String str)
    public boolean regionMatches(int toffset, String other, int ooffset,
            int len)
    public boolean regionMatches(boolean ignoreCase, int toffset,
            String other, int ooffset, int len)
    public boolean startsWith(String prefix, int toffset)
    public boolean startsWith(String prefix)
    public boolean endsWith(String suffix)
    public int hashCode()
    public int indexOf(int ch)
    public int indexOf(int ch, int fromIndex)
    private int indexOfSupplementary(int ch, int fromIndex)
    public int lastIndexOf(int ch)
    public int lastIndexOf(int ch, int fromIndex)
    private int lastIndexOfSupplementary(int ch, int fromIndex)
    public int indexOf(String str)
    public int indexOf(String str, int fromIndex)
    public int lastIndexOf(String str)
    public int lastIndexOf(String str, int fromIndex)
    public String substring(int beginIndex)
    public String substring(int beginIndex, int endIndex)
    public CharSequence subSequence(int beginIndex, int endIndex)
    public String concat(String str)
    public String replace(char oldChar, char newChar)
    public boolean contains(CharSequence s)
    public String replaceFirst(String regex, String replacement)
    public String replaceAll(String regex, String replacement)
    public String replace(CharSequence target, CharSequence replacement)
    public String[] split(String regex, int limit)
    public String[] split(String regex)
    public static String join(CharSequence delimiter, CharSequence... elements)
    public static String join(CharSequence delimiter,
            Iterable extends CharSequence> elements)
    public String toLowerCase(Locale locale)
    public String toLowerCase()
    public String toUpperCase(Locale locale)
    public String toUpperCase()
    public String trim()
    public String toString()
    public char[] toCharArray()
    public native String intern();
    

    メンバー関数には、length()文字列長、isEmpty()が空かどうか、charAt()インデックス文字、getChars()コピー、getBytes()符号化、equals()が等しいかどうか、compareTo()比較、startsWith()開始コンテンツマッチング、endsWith()ビームコンテンツマッチング、hashCode()hash値、indexOf()マッチング文字、文字列の先頭位置、substring()サブシーケンス、concat()接続、replace()置換、contains()は、split()分割、join()文字配列回転文字列、toLowerCase()回転大文字小文字、trim()脱先頭スペース、toString()文字列、toCharArray()回転文字配列を含む.
    static int indexOf(char[] source, int sourceOffset, int sourceCount,
            char[] target, int targetOffset, int targetCount,
            int fromIndex):
    for (int i = sourceOffset + fromIndex; i <= max; i++) {
        /* Look for first character. */
        if (source[i] != first) {
            while (++i <= max && source[i] != first);
        }
    
        /* Found first character, now look at the rest of v2 */
        if (i <= max) {
            int j = i + 1;
            int end = j + targetCount - 1;
            for (int k = targetOffset + 1; j < end && source[j]
                    == target[k]; j++, k++);
    
            if (j == end) {
                /* Found whole string. */
                return i - sourceOffset;
            }
        }
    }
    

    精髄のコードを比較して、whileサイクルとforサイクルは適切で、学ぶ価値があります.