StringBuilder introduction and related functions, StringBuilder converted into String 32

StringBuilder Introduction

  • StringBuilder variable is a string class, we can see it as a container, where the variable content refers StringBuilder object is variable.
    java.lang package under the category, without the leader packet.
  • StringBuilder is spliced ​​in a string object, modify operations, higher performance. So StringBuilder class suitable for stitching string \ modification operations
  • String class problem to do string concatenation and processing time are:
    each mosaic is going to be to abandon the original string object, but again point to a new string object. Waste of memory, produce too much garbage objects, create new objects, point to the new object, so the String class is not suitable for stitching string \ modify operation.
    StringBuilder stitching Case:
public class StringBuilderDemo01 {
    public static void main(String[] args) {
        String name = "赵丽颖";//不可变字符串
        name += "最美";
        name += "女演员";
        System.out.println(name);

        StringBuilder sb = new StringBuilder();
        sb.append("赵丽颖");
        sb.append("最美");
        sb.append("女演员");
        System.out.println(sb);
    }
}

StringBuilder constructor

  • public StringBuilder (); StringBuilder object to create a null string operation
  • public StringBuilder (String name); StringBuilder object to create an action string containing

What are the features StringBuilder

  • public StringBuilder append (of any type) splicing the new class object is connected after receiving
  • public StringBuilder reverse () string object inverse order
    Applications:
public class StringBuilderDemo02 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder(); //sb[""]
        sb.append("赵丽颖");
        sb.append("是演员");
        System.out.println(sb);

        StringBuilder sb1 = new StringBuilder("赵丽颖");
        sb1.append("是演员");
        System.out.println(sb1);

        StringBuilder sb2 = new StringBuilder(); //sb[""]
        sb2.append("赵丽颖").append("是").append("演员").append(12).append(true);//链式编程
        System.out.println(sb2);

        sb2.reverse();//对象内容反序
        System.out.println(sb2);
    }
}

StringBuilder converted into a String

  • StringBuilder splicing is only a means to manipulate strings. (means)

  • String result in development or to be assigned to the String type (purpose)

  • String S tringBuilder converted directly into the like append

  • StringBuilder eventually converted into a String must call toString () method
    -public String toString ()

Case:

public class StringBuilderDemo03 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("赵丽颖");
        sb.append("是演员").append("123").reverse();

        System.out.println(sb);

        //开发中很多时候字符串的结果还是要赋值给String类型的
        //StringBuilder() => String类型
        String result = sb.toString();//把StringBuilder拼接的字符结果又转换成字符串类的数据
        System.out.println(result);
        test(result);//现在就可以调用test了,不然不行的不然是StringBuilder类型不是String类型

    }

    public static void test(String name){

    }
}

Note: String type String type can only be used to accept, not to accept the need to use the toString conversion format with StringBuilder.

Published 34 original articles · won praise 16 · views 272

Guess you like

Origin blog.csdn.net/qq_41005604/article/details/105297794