javaj basis (String and StringBuilder --- immutable and mutable sequence of characters using traps)

String:

  StringClass represents character strings. All string literal Java program (e.g. "abc") are implemented as an instance of this class.

  Strings are constant; their values ​​can not be changed after creation. String buffer supports variable strings. Because String objects are immutable, so you can share.

StringBuilder/StringBuffer:

  A variable sequence of characters. Stringbuilder unsafe, but fast (common), StringBuffer safe and slow.

 

 Use String string splicing :( do not recommend consuming time and space)

  Example:

  String str = "";

  for (int i = 0; i <5000; i ++) {
    STR STR + = i;   equivalent to 10,000 objects generated object i, str an object

  }

 Use StringBuilder for string splicing:

  StringBuilder str = new StringBuilder("");

  for(itn i = 0;i < 5000;i++){

    str.append (i);  save a lot of time and space

  }

    

 

Guess you like

Origin www.cnblogs.com/skyline1/p/11139377.html