java CharSequence、String、StringBuffer、StringBuilder详解

       CharSequence、String、StringBuffer、StringBuilder详解:

      String 继承于CharSequence,也就是说String也是CharSequence类型。除了String实现了CharSequence之 外,StringBuffer和StringBuilder也实现了CharSequence接口。需要说明的是,CharSequence就是字符序 列,String, StringBuilder和StringBuffer本质上都是通过字符数组实现的。

      继承关系如下图 :

             java CharSequence、String、StringBuffer、StringBuilder详解    

 

       CharSequence 是一个接口,它只包括length(), charAt(int index), subSequence(int start, int end),toString()这四个方法。代码如下:

      CharSequence源码(基于jdk1.7.40)

         package java.lang;
            public interface CharSequence {
                        int length();
                        char charAt(int index);
                        CharSequence subSequence(int start, int end);
                        public String toString();
            }

      String类源码如下:(基于jdk1.7.40)

public    String()
public    String(String original)
public    String(char[] value)
public    String(char[] value, int offset, int count)
public    String(byte[] bytes)
public    String(byte[] bytes, int offset, int length)
public    String(byte[] ascii, int hibyte)
public    String(byte[] ascii, int hibyte, int offset, int count)
public    String(byte[] bytes, String charsetName)
public    String(byte[] bytes, int offset, int length, String charsetName)
public    String(byte[] bytes, Charset charset)
public    String(byte[] bytes, int offset, int length, Charset charset)
public    String(int[] codePoints, int offset, int count)
public    String(StringBuffer buffer)
public    String(StringBuilder builder)
public char    charAt(int index)
public int    codePointAt(int index)
public int    codePointBefore(int index)
public int    codePointCount(int beginIndex, int endIndex)
public int    compareTo(String anotherString)
public int    compareToIgnoreCase(String str)
public String    concat(String str)
public boolean    contains(CharSequence s)
public boolean    contentEquals(StringBuffer sb)
public boolean    contentEquals(CharSequence cs)
public static String    copyValueOf(char[] data, int offset, int count)
public static String    copyValueOf(char[] data)
public boolean    endsWith(String suffix)
public boolean    equals(Object anObject)
public boolean    equalsIgnoreCase(String anotherString)
public static String    format(String format, Object[] args)
public static String    format(Locale l, String format, Object[] args)
public int    hashCode()
public int    indexOf(int ch)
public int    indexOf(int ch, int fromIndex)
public int    indexOf(String str)
public int    indexOf(String str, int fromIndex)
public String    intern()
public int    lastIndexOf(int ch)
public int    lastIndexOf(int ch, int fromIndex)
public int    lastIndexOf(String str)
public int    lastIndexOf(String str, int fromIndex)
public int    length()
public boolean    matches(String regex)
public int    offsetByCodePoints(int index, int codePointOffset)
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 String    replace(char oldChar, char newChar)
public String    replace(CharSequence target, CharSequence replacement)
public String    replaceAll(String regex, String replacement)
public String    replaceFirst(String regex, String replacement)
public String[]    split(String regex, int limit)
public String[]    split(String regex)
public boolean    startsWith(String prefix, int toffset)
public boolean    startsWith(String prefix)
public CharSequence    subSequence(int beginIndex, int endIndex)
public String    substring(int beginIndex)
public String    substring(int beginIndex, int endIndex)
public char[]    toCharArray()
public String    toLowerCase(Locale locale)
public String    toLowerCase()
public String    toString()
public String    toUpperCase(Locale locale)
public String    toUpperCase()
public String    trim()
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    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)
public void    getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
public byte[]    getBytes(String charsetName)
public byte[]    getBytes(Charset charset)
public byte[]    getBytes()
public void    getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
public boolean    isEmpty()

StringBuilder类源码如下:(基于jdk1.7.40) 

StringBuilder()
StringBuilder(int capacity)
StringBuilder(CharSequence seq)
StringBuilder(String str)
StringBuilder     append(float f)
StringBuilder     append(double d)
StringBuilder     append(boolean b)
StringBuilder     append(int i)
StringBuilder     append(long l)
StringBuilder     append(char c)
StringBuilder     append(char[] chars)
StringBuilder     append(char[] str, int offset, int len)
StringBuilder     append(String str)
StringBuilder     append(Object obj)
StringBuilder     append(StringBuffer sb)
StringBuilder     append(CharSequence csq)
StringBuilder     append(CharSequence csq, int start, int end)
StringBuilder     appendCodePoint(int codePoint)
int     capacity()
char     charAt(int index)
int     codePointAt(int index)
int     codePointBefore(int index)
int     codePointCount(int start, int end)
StringBuilder     delete(int start, int end)
StringBuilder     deleteCharAt(int index)
void     ensureCapacity(int min)
void     getChars(int start, int end, char[] dst, int dstStart)
int     indexOf(String subString, int start)
int     indexOf(String string)
StringBuilder     insert(int offset, boolean b)
StringBuilder     insert(int offset, int i)
StringBuilder     insert(int offset, long l)
StringBuilder     insert(int offset, float f)
StringBuilder     insert(int offset, double d)
StringBuilder     insert(int offset, char c)
StringBuilder     insert(int offset, char[] ch)
StringBuilder     insert(int offset, char[] str, int strOffset, int strLen)
StringBuilder     insert(int offset, String str)
StringBuilder     insert(int offset, Object obj)
StringBuilder     insert(int offset, CharSequence s)
StringBuilder     insert(int offset, CharSequence s, int start, int end)
int     lastIndexOf(String string)
int     lastIndexOf(String subString, int start)
int     length()
int     offsetByCodePoints(int index, int codePointOffset)
StringBuilder     replace(int start, int end, String string)
StringBuilder     reverse()
void     setCharAt(int index, char ch)
void     setLength(int length)
CharSequence     subSequence(int start, int end)
String     substring(int start)
String     substring(int start, int end)
String     toString()
void     trimToSize()

 StringBuffer类源码如下:(基于jdk1.7.40)

StringBuffer()
StringBuffer(int capacity)
StringBuffer(String string)
StringBuffer(CharSequence cs)

StringBuffer     append(boolean b)
StringBuffer     append(int i)
StringBuffer     append(long l)
StringBuffer     append(float f)
StringBuffer     append(double d)
synchronized StringBuffer     append(char ch)
synchronized StringBuffer     append(char[] chars)
synchronized StringBuffer     append(char[] chars, int start, int length)
synchronized StringBuffer     append(Object obj)
synchronized StringBuffer     append(String string)
synchronized StringBuffer     append(StringBuffer sb)
synchronized StringBuffer     append(CharSequence s)
synchronized StringBuffer     append(CharSequence s, int start, int end)
StringBuffer     appendCodePoint(int codePoint)
int     capacity()
synchronized char     charAt(int index)
synchronized int     codePointAt(int index)
synchronized int     codePointBefore(int index)
synchronized int     codePointCount(int beginIndex, int endIndex)
synchronized StringBuffer     delete(int start, int end)
synchronized StringBuffer     deleteCharAt(int location)
synchronized void     ensureCapacity(int min)
synchronized void     getChars(int start, int end, char[] buffer, int idx)
synchronized int     indexOf(String subString, int start)
int     indexOf(String string)
StringBuffer     insert(int index, boolean b)
StringBuffer     insert(int index, int i)
StringBuffer     insert(int index, long l)
StringBuffer     insert(int index, float f)
StringBuffer     insert(int index, double d)
synchronized StringBuffer     insert(int index, char ch)
synchronized StringBuffer     insert(int index, char[] chars)
synchronized StringBuffer     insert(int index, char[] chars, int start, int length)
synchronized StringBuffer     insert(int index, String string)
StringBuffer     insert(int index, Object obj)
synchronized StringBuffer     insert(int index, CharSequence s)
synchronized StringBuffer     insert(int index, CharSequence s, int start, int end)
int     lastIndexOf(String string)
synchronized int     lastIndexOf(String subString, int start)
int     length()
synchronized int     offsetByCodePoints(int index, int codePointOffset)
synchronized StringBuffer     replace(int start, int end, String string)
synchronized StringBuffer     reverse()
synchronized void     setCharAt(int index, char ch)
synchronized void     setLength(int length)
synchronized CharSequence     subSequence(int start, int end)
synchronized String     substring(int start)
synchronized String     substring(int start, int end)
synchronized String     toString()
synchronized void     trimToSize()

你可能感兴趣的:(java,String)