JAVA常用类--------String,StringBuffer,StringBulide

1.String

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];

特点:支持序列化,可以比较。内容不可变。

易错点:

1.string 构造方法

 public static void main(String[] args) {
        String str = "dsdsd";
        String str1 = new String("dsdsd");
        System.out.println(str == str1);    //结果为false
    }

图解

 2.string 赋值

 public static void main(String[] args) {
        String s1 = "JAVA";
        String s2 = "GOOD";

        String s3 = "JAVAGOOD";
        String s4 = "JAVA"+"GOOD"; 
        String s5 = s1 + "GOOD";    //等价于new的方式
        String s6 = "JAVA" + s2;

        System.out.println(s3 == s4);       //true
        System.out.println(s3 == s5);       //false
        System.out.println(s3 == s6);       //false
        System.out.println(s5 == s6);       //false
    }

图解

 

 3.小面试题

package com.xjq.usedObject;

public class StringDemo01 {
    String s1 = "test ok";
    char[] chars = {'t','e','s','t'};

    public  void change(String s1,char[] chars){
        s1 = "test good";
        chars[0] = 'b';
    }
    public static void main(String[] args) {

        StringDemo01 stringDemo01 = new StringDemo01();
        stringDemo01.change(stringDemo01.s1,stringDemo01.chars);
        System.out.println(stringDemo01.s1);   //test ok
        System.out.println(stringDemo01.chars);   //best

    }
}

 解析:由于string的不可变性,作为值传递时,不会改变原来的值。

 

string 常用方法

 

 

 String类型和基本数值类型的转换

string - -->基本数据类型         基本数值类型.parseXXX()

基本数据类型------->String              String.vaueof()

 String------->char[]                       toCharsArray()

char[] --------->String                     new String(char[])

String ------>bytes[]                             getBytes()

bytes[] --------String                         new String(bytes[])

 

 String   StringBuffer   StringBuilder三者的异同点。

String: 不可变字符序列,jdk1.0,底层使用char[]存储

String str = new String() --->char[] chars = new char[0];

StringBuffer: 可变字符序列。线程安全,效率低  jdk1.0  底层使用char[]存储

StringBuilder:可变字符序列。线程不安全,效率高。 jdk1.5底层使用char[]存储

StringBuffer和StringBuilder在创建时:

StringBuffer buf = new StringBuffer() ------>char[] chars =  new  chars[16];

当StringBuffer和StringBuilder达到默认的长度时,会进行自动扩容;会创建一个新的char[] ,长度为原来长度的二倍+2,然后把原数组的内容进行复制,在进行添加。

  public AbstractStringBuilder append(String str) {
        if (str == null)
            return appendNull();
        int len = str.length();
        ensureCapacityInternal(count + len);
        str.getChars(0, len, value, count);
        count += len;
        return this;
    }


  */
    private void ensureCapacityInternal(int minimumCapacity) {
        // overflow-conscious code
        if (minimumCapacity - value.length > 0) {
            value = Arrays.copyOf(value,
                    newCapacity(minimumCapacity));
        }
    }

 StringBuffer常用类

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值