1.== and equals
1) a==b is an address comparison, and a.equals (b) is a comparison of values.
2) The String class is the final immutable class, and a change to string will cause a new string object to be generated.
3) The "ABC" form is assigned to the string constant pool.
4) The JVM joins the "+" sign of the string constant, which optimizes the "+" connection of the constant string to the concatenated value.
5) If a reference exists during the string + operation, such as t= "a"; b = t+ "BC"; t in the run-time to get the T memory block, it is not optimized to "ABC" constant. But a final in front of the variable is also stored in a constant pool or embedded in its byte stream at compile time.
2. Splicing with StringBuilder with string concatenation
3.String, StringBuilder, StringBuffer
When the calculation is not very high, you can not consider the performance problem;
Single-threaded, when the amount of computation millions, choose StringBuilder;
When multi-threading, when the amount of computation millions, choose StringBuffer;
Package Day_3.string;public class TestString {static void test_1 () {int I=0;while (true) {String a = "abc";//string a = new String ("abc"); try {thread.sleep (10); System.out.println (i++);} catch (Interruptedexception e) {e.printstacktrace ();} A = null;}} /** * 1) a==b is an address comparison, A.equals (b) is a value comparison. 2) The String class is the final immutable class, and a change to string will cause a new string object to be generated. 3) The "ABC" form is assigned to the string constant pool. */static void Test_2 () {//string a = "abc";//string b = "abc";//SYSTEM.OUT.PRINTLN ("A==b:" + (a==b)); True//system.out.println ("A.equals (b):" + a.equals (b)); True//string a = new String ("abc"); String b = new String ("abc"); System.out.println ("A==b:" + (a==b)); False//system.out.println ("A.equals (b):" + a.equals (b)); TrueString a = "abc"; String b = new String ("abc"); System.out.println ("A==b:" + (a==b)); FalseSystem.out.println ("A.equals (b):" + a.equals (b)); true}/** * 4) The JVM joins the "+" sign of the string constant, which optimizes the "+" connection of the constant string to the concatenated value. * 5) If a reference exists during the string + operation, such as t= "a"; b = t+ "BC"; t in the run time to get T memory block, obviously not optimized to "ABC" constant. But put a final in front of the variable,It is also stored as a constant pool or embedded in its byte stream during the compilation period. */static void Test_3 () {String a = "abc"; String B = "a" + "BC"; System.out.println ("A==b:" + (a==b)); TrueSystem.out.println ("A.equals (b):" + a.equals (b)); True//string a = "abc"; String t = "a"; String b = t+ "BC";//system.out.println ("A==b:" + (a==b)); False//system.out.println ("A.equals (b):" + a.equals (b)); True//string a = "abc"; Final String t= "a"; String b = t+ "BC";//system.out.println ("A==b:" + (a==b)); True//system.out.println ("A.equals (b):" + a.equals (b)); true}static void Test_4 () {String str= "1234567890 Shanghai"; int len = Str.length (); System.out.println ("len =" + len); String str2 = "Hello," + "java" + "!" + len; System.out.println ("str2 =" + str2);} /** * string, StringBuilder 1) string refers to the space thrown away, open up new space to hold the string (invariant Class), the memory fragment will occur, 2) StringBuilder first opened the buffer, append () in the buffer operation, Memory efficiency is greatly improved (variable Class). */static void Test_5 () {System.out.println ("Pre-run memory available" + runtime.getruntime (). Freememory ()); String s = ""; String newstr = new String ("abc"); StringBuilderBuilder = new StringBuilder (+), for (int i = 0; i <; i++) {//s = s + newstr;//Memory Remains down Builder.append (Newst R); The available memory remains the same if (i% = = 0) {System.out.println ("Running memory available" + runtime.getruntime (). Freememory ()); }}}/** * StringBuilder, StringBuffer 1) Similarities: No memory fragmentation occurs when strings are concatenated, 2) Differences: The StringBuilder class is not thread-safe, but its performance in a single thread is higher than stringbuffer. */static void Test_6 () {StringBuilder strbuilder = new StringBuilder (); String str = new string (); StringBuffer strbuffer = new StringBuffer (); System.out.println (System.currenttimemillis ()); for (int i = 0; i < 1000000; i++) {//str = str + "add";//Strbuild ER = strbuilder.append ("add"); Strbuffer = Strbuffer.append ("Add");} System.out.println (System.currenttimemillis ());} public static void Main (string[] args) {test_6 ();}}
Java_string, Collection Class