search

Home  >  Q&A  >  body text

stringbuilder - java StringBuilding 定义

这2种定义方式 有不同吗??
第一种会慢吗?

PHP中文网PHP中文网2814 days ago744

reply all(6)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:20:25

    Answer: Just as fast

    String initialization operation:

    String s= "a"+"b";
    String s1= "ab";
    

    This way the compiler is the same by default. So it won’t be slow

    Another example:

      String a= "a";
      String b = "b";
      String s2= a+b;
    

    The s2 produced like this is very slow because the memory is requested three times.

    I hope you will adopt it and give me points if you are satisfied. . I am a new member and hope to serve you all! ! ! (I really want to give -1 point to the first answer, just give it a few random answers)

    reply
    0
  • 阿神

    阿神2017-04-18 10:20:25

    The first type is not just a slow problem, but a code that looks like shit.

    new StringBuilder("a").append("b").append("c");

    new StringBuilder("a" + "b" + "c");

    If you use it like the above, it is better to directly use "a" + "b" + "c"因为没有使用到StringBuilder的任何特性,加上JDK’s compilation and optimization. The final code will become as follows.

    new StringBuilder(new StringBuilder("a").append("b").append("c").toString());

    I don’t understand the second one at all. What is the meaning of using StringBuilder? Isn't it just a string in itself?

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:20:25

    javap -c看看,编译器应该会把你用+The connected string constants are spliced ​​together, and the two writing methods should run the same.

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:20:25

    Constants can be used directly +
    Variables can also be used +
    If you care about performance, check this out: http://calvin1978.blogcn.com/...

    reply
    0
  • PHPz

    PHPz2017-04-18 10:20:25

    As long as the splicing character '+' is not used between objects, there is basically no difference between using the splicing character '+' between strings and not using it; and once the string object is generated, it will be modified to generate another object with all strings Splicing between objects will be very slow;

    public static void main(String[] args) {

    fun1(10000000);

    }
    private static void fun1(int length){

    long min1 = new Date().getTime();
    System.out.println(min1);
    //1
    for(int i=0;i<length;i++){
        new StringBuffer("字符1;" +
                "字符2;" +
                "字符3;" +
                "字符4;" +
                "字符5;");
    }
    long min2 = new Date().getTime();
    System.out.println(min2);
    System.out.println(min2-min1);

    }
    private static void fun2(int length){

    long min1 = new Date().getTime();
    System.out.println(min1);
    //2
    for(int i=0;i<length;i++){
        new StringBuffer("字符1;字符2;字符3;字符4;字符5;");
    }
    long min2 = new Date().getTime();
    System.out.println(min2);
    System.out.println(min2-min1);

    }
    private static void fun3(int length){

    long min1 = new Date().getTime();
    System.out.println(min1);
    //3
    for(int i=0;i<length;i++){
        String aa = "字符1;" +
        "字符2;" +
        "字符3;" +
        "字符4;" +
        "字符5;";
        new StringBuffer(aa);
    }
    long min2 = new Date().getTime();
    System.out.println(min2);
    System.out.println(min2-min1);

    }
    private static void fun4(int length){

    long min1 = new Date().getTime();
    System.out.println(min1);
    //3
    for(int i=0;i<length;i++){
        String aa = "字符1;" ;
        aa +="字符2;" ;
        aa +="字符3;" ;
        aa +="字符4;" ;
        aa +="字符5;";
        new StringBuffer(aa);
    }
    long min2 = new Date().getTime();
    System.out.println(min2);
    System.out.println(min2-min1);

    }

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:20:25

    These two methods of creating objects are faster and slower. In fact, they can be converted into the following two methods to compare faster and slower:

    String a = "Test";
    String b = "T" + "e" + "s" + "t";

    Of course these two methods are equally fast. In the JVM, "T" + "e" + "s" + "t" are actually processed as "Test".
    Let’s now take a look at the following string splicing method:

    String a = "Te";
    String b = a + "st";

    The above method is the least efficient method of string splicing and is extremely not recommended, because when this kind of string splicing is done, the JVM actually generates a new StringBuilder, then calls the append method, and then performs toString. The speed is simply It’s a slow dog (dog: I’m lying on the gun again...)
    So to sum up: the two new StringBuilder methods written by the subject are actually the same method, the efficiency is the same, and they are straightforward.

    reply
    0
  • Cancelreply