First, what is a string
1. A string object or its equivalent object (such as a char array) that always occupies the maximum space in memory fast
2.String and char arrays
Char array + offset + length = String
Three basic features of 3.String
- Invariance: Once defined, cannot be changed
- optimization for a constant pool: only the same copy of a constant pool is referenced when two string objects have the same value
- Final definition of class
Listing 1. Sample code
1 Public classStringdemo {2 Public Static voidMain (string[] args) {3String str1 = "abc";4String str2 = "abc";5String STR3 =NewString ("ABC");6String STR4 =str1;7 //str1, str2, STR4 refer to the same address, but STR3 re-opened up a memory space,8 //Although STR3 takes up heap space alone, it points to the same entity as the str19System.out.println (STR1==STR2);//trueTenSystem.out.println (STR1==STR3);//false OneSystem.out.println (Str1.intern () ==str3.intern ());//true ASystem.out.println (STR1==STR4);//true -System.out.println (STR2==STR4);//true -System.out.println (Str4.intern () ==str3.intern ());//true the } -}
Second, the discussion of splitting the string way
- The split method supports passing in regular expressions to help with strings, but the disadvantage is that the algorithm it relies on has poor performance when splitting simple strings.
- The StringTokenizer class uses its NextToken () method to get the next segmented string, and the Hasmoretoken method lets you know if there are more strings to handle.
The comparison found that split takes a very long time and is handled quickly with StringTokenizer objects.
- To implement the string segmentation algorithm by itself, the string segmentation algorithm combined with the substring method and the IndexOf method can help to quickly slice and replace the string.
Iii. Merging objects
String objects are immutable, strings are spliced, modified, new objects are generated, poor performance
But the JVM will optimize for a+b+c+d, which will only produce a final new string.
String object joins, joins using the Concat method, uses the StringBuilder class, and many other ways
1 Public Static voidMain (string[] args) {2 //TODO auto-generated Method Stub3String str =NULL;4String result = "";5 LongStart =System.currenttimemillis ();6 for(inti = 0; I < 10000; i++) {7str = str +i;8 }//The first method compiler determines that the addition of a String is run as a StringBuilder implementation9 LongEnd =System.currenttimemillis ();//But the compiler did not make enough smart judgments, and each cycle generated a new StringBuilder instance that greatly reduced system performance. TenSYSTEM.OUT.PRINTLN (End-start); The longest time OneStart =System.currenttimemillis (); A for(inti = 0; I < 10000; i++) { -result =Result.concat (string.valueof (i)); - } theEnd =System.currenttimemillis (); -SYSTEM.OUT.PRINTLN (End-start); The time of the undersecretary -Start =System.currenttimemillis (); -StringBuilder SB =NewStringBuilder (); + for(inti = 0; I < 10000; i++) { - sb.append (i); + } AEnd =System.currenttimemillis (); atSYSTEM.OUT.PRINTLN (End-start); Shortest time -}
StringBuffer almost all of the methods are synchronized to ensure thread safety, but consume a certain amount of system resources
StringBuilder Conversely, not synchronized, unsafe, high efficiency
They have something in common
The expansion strategy is to double the original capacity, request the memory space with the new capacity, create a new char array, and then copy the contents of the original array into the new array. Therefore, the expansion of large objects involves a large amount of memory replication operations. Performance can be improved if the size is pre-evaluated.
Iv. data definition and optimization of operational logic
http://mp.weixin.qq.com/s?__biz=MjM5NzMyMjAwMA==&mid=207904935&idx=1&sn= 54e087dbe4ebd4aad9fac019754d1b37&scene=23&srcid=0903qqgf7yvkvi6tbfrs8pni#rd
Java Program Optimization: string manipulation, basic operation methods and other optimization strategies are not completed