java String、StringBuffer

104 阅读3分钟

String内存分析

image.png image.png

//字符串   String类代表字符串
String str = new String("asd");
String str2 = "张三";
String str3 = "张三";
String str4 = new String("张三");
String str5 = new String("张三");
System.out.println(str2);
System.out.println(str2.equals(str3));  //true
System.out.println(str2 == str3);   //true
System.out.println("---------");
//equals是比较字符串的值
System.out.println(str2.equals(str4));  //true
// 如果字符串是通过new 去实例化出来的,那么 == 会比较内存地址
System.out.println(str2 == str4);   //false,因为值一样,但是内存地址不一样
System.out.println(str4 == str5);   //false,因为值一样,但是内存地址不一样

System.out.println("<---------->");
String a = new String("ab");
String b = "ab";
String add = a +"cd";
String add2 = b +"cd";
String c = "ab" + "cd";
String d = "abcd";
System.out.println(add == d);   //false
System.out.println(add2 == d);  //false
System.out.println(c == d); //true

String 方法

image.png

//定义获取变量的函数方法
public static String getType(Object o) {
    return o.getClass().getName();
}

public static void main(String[] args) {
    //boolean endsWith(String str)  是否以指定字符结束
    String s1 = new String("asd");
    System.out.println(s1.startsWith("a")); //true
    System.out.println(s1.endsWith("s"));   //false
    System.out.println("<-------------->");

    //boolean isEmpty()     长度是否为0
    String s2 = "";
    System.out.println(s2.isEmpty()); //true
    System.out.println("<-------------->");


    //boolean contains(CharSequences)     是否包含指定序列  应用:搜索
    String s3 = "fgfdgsdfg";
    System.out.println(s3.contains("fdg")); //true
    System.out.println("<-------------->");

    //boolean equals(Object anObject)   字符串值是否相等
    String s4 = "qazQ";
    String s5 = new String("qazq");
    System.out.println(s4.equals(s5)); //false
    System.out.println("<-------------->");

    //boolean equalsIgnoreCase(String anotherString )   忽略大小写的情况,比较字符串值是否相等
    String s6 = "qazQ";
    String s7 = new String("qazq");
    System.out.println(s4.equalsIgnoreCase(s5)); //true
    System.out.println("<-------------->");

    
    //String(char[] value)  将字符数组转换成字符串
    //String(char[] value, int offset, int count)
    char[] arry = {'h','e','l','l','o'};
    String st = new String(arry);
    String st2 = new String(arry,1,3);
    System.out.println(st); //hello
    System.out.println(st2); //ell
    System.out.println("<-------------->");


    int a0 = 10;
    System.out.println((double)a0);//10.0

    //String.valueOf    将基本数据型态转换成 String    String a1 = String.valueOf(a0);
    String a1 = String.valueOf(a0);
    System.out.println(getType(a0));   //java.lang.Integer
    System.out.println(getType(a1));    //java.lang.String

    //Double.parseDouble(String s) : 将 s 转换成 double     double retval = Double.parseDouble(h1);
    String h1 = "11";
    double retval = Double.parseDouble(h1);
    System.out.println(getType(retval));    //java.lang.Double
    System.out.println("<-------->");

    // char[] toCharArray()     将字符串转换成字符数组
    String ak = "abdfdfwds";
    char[] k1 = ak.toCharArray();   //将字符串转换成字符数组
    System.out.println(getType(k1));    //[C
    System.out.println(getType(ak));    // java.lang.String
    System.out.println("<----------->");

    //String replace(char old, char new)  替换
    ak = ak.replace("d","$");
    System.out.println(ak);     //$f$fw$s

    //String[] split(String regex)    切割
    String[] ar = ak.split("w|b");
    for (int i = 0; i < ar.length; i++) {
        System.out.println(ar[i]);
    }
    System.out.println("<----------->");

    //String substring(int beginindex)    截取字符串
    //String substring(int beginindex, int endindex)    截取字符串

    String book = "1234567if";
    String b1 = book.substring(3);  //34567if   从下标开始切割,取右边
    String b2 = book.substring(5,8);  //567     左边闭区间,右边开区间  --》[5 ,8)
    System.out.println(b1);
    System.out.println(b2);
    System.out.println("<----------->");

    //String toUpperCase()    转大写
    //String tolowerCase()    转小写
    String cook = "qaz";
    System.out.println(cook.toUpperCase());     //转大写
    System.out.println(cook.toLowerCase());     //转小写
    System.out.println("<----------->");

    //String trim()   去空格
    String dark = "  aa";
    String dark1 = " a a";
    String dark2 = "  a ad  f";
    System.out.println(dark.trim()); //aa
    System.out.println(dark1.trim()); //a a
    System.out.println(dark2.replace(" ","")); //aadf      按理来说,这才是真的去空格
    System.out.println("<----------->");

    //字符串运算
    String food = "fgfd";
    String food2 = food+100;    //String 空间没有限制大小,int空间固定,100自动转换成字符串去拼接
    System.out.println(food2);  //fgfd100


}

StringBuffer

由于String是不可变的,所以导致String对象泛滥,在频繁盖面字符串对象的应用中,需要使用可变的StringBuffer类
常见方法: image.png

public static void main(String[] args) {
    StringBuffer buff = new StringBuffer();
    //buff比较的是地址
    System.out.println(buff.equals(""));    //false
    System.out.println(buff.equals(buff));  //true

    buff.append("A").append("fdfeWE");
    System.out.println(buff);       //AfdfeWE

    buff.insert(2,"***");
    System.out.println(buff);   //Af***dfeWE

    System.out.println(buff.toString().equals("Af***dfeWE"));   //true
    System.out.println(buff.indexOf("d"));  //5
    System.out.println(buff.substring(0,2));    //Af
    System.out.println(buff.replace(0,3,"h"));    //h**dfeWE
    buff.setCharAt(5,'H');
    System.out.println(buff);   //h**dfHWE
    System.out.println(buff.reverse()); //EWHfd**h

    //StringBuffer  速度慢,线程安全
    //Stringbuilder  速度快,线程不安全
}

效率对比


//        //获取当前的时间的毫秒
//        long startTime = System.currentTimeMillis();
//        String s ="sd";
//        for (int i = 0; i < 100000; i++) {
//           s =s + i;
//        }
//        long endTime = System.currentTimeMillis();
//        System.out.println(endTime - startTime);    //118410-19780之间

        //获取当前的时间的毫秒
        long startTime = System.currentTimeMillis();
        StringBuilder str = new StringBuilder("sd");
        for (int i = 0; i < 100000; i++) {
            str.append(i);
        }
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime);    //12-13毫秒之间

参考:
String对象内存分配分析