`
hui_jing_880210
  • 浏览: 43617 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java CharSequence、String、StringBuffer、StringBuilder详解

    博客分类:
  • java
阅读更多

       CharSequence、String、StringBuffer、StringBuilder详解:

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

      继承关系如下图 :

                

 

       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、StringBuilder和StringBuffer的区别详解

    Java中的`String`、`StringBuilder`和`StringBuffer`都是用来处理字符串的类,它们各自有不同的特性和适用场景。在理解它们之间的区别之前,我们首先要知道它们的共同点。 **共同点:** 1. **都是字符串类**:这三...

    java StringBuffer详解

    `StringBuffer`是Java中处理字符串的类,它继承自`AbstractStringBuilder`并实现了`Serializable`和`CharSequence`接口。它的主要功能是在字符串操作(如拼接、插入、删除等)过程中提供高效且线程安全的操作。 2....

    【Java编程教程】详解Java String字符串.pdf

    `CharSequence`是一个接口,代表字符序列,String、StringBuffer和StringBuilder都是它的实现者,这意味着可以使用这些类来处理字符序列。 `String`类提供了大量方法来操作字符串,例如: 1. `compare()`:比较两个...

    详解Java中字符串缓冲区StringBuffer类的使用

    在Java编程中,`StringBuffer`类是一个非常重要的工具,尤其在处理字符串时需要频繁修改字符串内容的情况下...在处理字符串时,根据具体需求选择`String`、`StringBuilder`或`StringBuffer`,能够有效地优化程序性能。

    java源码解读由浅入深.pdf

    ### Java源码解读之String类详解 #### 一、引言 在Java开发过程中,`String`类无疑是最常用的数据类型之一。它不仅在日常编码中频繁出现,也是面试中的热门话题。本文将深入探讨`String`类的核心实现机制及其重要...

    string类的常用方法.pdf

    因此,在需要频繁修改字符串的情况下,推荐使用`StringBuilder`或`StringBuffer`类,这两个类提供了可变的字符串实现,能够有效减少对象创建带来的开销。 ### 结论 了解并熟练掌握`String`类的各种方法对于Java...

    Java字符串的10个入门知识.docx

    ### Java字符串的10个入门知识详解 #### 一、基本数据类型与字符串之间的转换 在Java编程中,经常需要进行基本数据类型与字符串之间的相互转换。这些转换可以通过多种方式进行。 1. **基本数据类型转字符串** - ...

    基于StringBuilder类中的重要方法(介绍)

    StringBuilder类是Java中一个非常重要的类,用于字符串的操作和处理。下面我们将对StringBuilder类中的重要方法进行详解。 append方法 append方法用于将参数的字符串表示形式追加到StringBuilder对象中。...

    面试宝典新版.docx

    Java 基础部分知识点详解 1. JDK与JRE的区别 JDK(Java Development Kit)是Java开发工具包,包含开发和运行Java程序所需的所有工具,如Javac编译器、Java运行环境(JRE)以及各种调试和分析工具。JRE仅提供运行...

    一些常用的String类方法

    在处理字符串时,要注意字符串的不可变性,以及在拼接字符串时,尽量避免使用 `+` 运算符,而是选择 `StringBuilder` 或 `StringBuffer`。此外,字符串常量池的概念对于理解字符串的内存管理至关重要。在JDK 1.8之后...

    android面试题汇总(含答案)

    **解析:** 在Java中,`String`、`StringBuffer`和`StringBuilder`都是用来表示文本字符串,但它们之间的主要区别在于: - **String:** 不可变字符串,每次修改都会创建新的字符串对象。 - **StringBuffer:** 线程...

Global site tag (gtag.js) - Google Analytics