Strings are also frequently used in programming.
1) immutable
In fact, the view API will find:
Public final class Stringextends objectimplements Serializable, Comparable<string>, charsequence
Final, immutable.
public class TestString { static string upcase (String s) { return s.touppercase (); } public static void Main (string[] args) { String s = "BBC"; String SS = UpCase (s); System.out.println (s); }}
Although the S-pass gives the UpCase method, but s does not change, when the parameter is passed, s copies a reference, but the object referred to by the reference does not move.
2) Reload "+" with StringBuilder
In fact, StringBuilder and stringbuffer things, in the interview pen questions have been seen many times, but, but, each time will forget, this property of things, although a test can know the efficiency problem, but directly answer or by virtue of memory. Some time ago in the understanding of a foreign work in the female programmer's paper with the memory of English, then remember, the answer is now not found.
public class test{ public static void Main (string[] args) { String s = "BBC"; String SS = "DDT" +s+ "SDF" +3; System.out.println (ss); }}f:\>javac test.javaf:\>javap-c testcompiled from ' Test.java ' public class Test {public test (); code:0: Aload_0 1:invokespecial #1//Method java/lang/object. " <init> ":() V 4:return public static void Main (java.lang.string[]); code:0: LDC #2//String BBC 2:astore_1 3:new #3 Class Java/lang/stringbuilder 6:dup 7:invokespecial #4//Method Java/lang/stringbuild Er. " <init>:() V 10:LDC #5//String DDT 12:invokevirtual #6Method java/lang/stringbuilder.append: (ljava/lang/string;) Ljava/lang/stringbuilder; 15:aload_1 16:invokevirtual #6//Method java/lang/stringbuilder.append: (ljava/lang/string;) ljava/ Lang/stringbuilder; 19:LDC #7//String SDF 21:invokevirtual #6//Method JAVA/LANG/STRINGB Uilder.append: (ljava/lang/string;) Ljava/lang/stringbuilder; 24:iconst_3 25:invokevirtual #8//Method Java/lang/stringbuilder.append: (I) Ljava/lang/stringbuild Er 28:invokevirtual #9//Method java/lang/stringbuilder.tostring: () ljava/lang/string; 31:astore_2 32:getstatic #10//Field Java/lang/system.out:ljava/io/printstream; 35:aload_2 36:invokevirtual #11//Method java/io/printstream.println: (ljava/lang/string;) V 39 : return}
The author is really powerful, anti-compilation class, come out a lot of compilation, previously learned 8086 of the look is quite familiar, the code found itself called StringBuilder. Because with the idea that the author said, if + is a string of append repeatedly, then often add one after the new object, and finally produce a lot of useless objects.
Even if the default call to StringBuilder, then is it possible to use all the overloaded +?
public class test{ public string Plus (string[] array) { string s = null; for (int i = 0; i < array.length; i++) { s + = array[i]; } return s; } public String Builder (string[] array) { StringBuilder sb = new StringBuilder (); for (int i = 0; i < ARRA Y.length; i++) { sb.append (Array[i]); } return sb.tostring (); }} Decompile: Compiled from ' Test.java ' public class Test {public test (); code:0: Aload_0 1:invokespecial #1 Method java/lang/object. " <init> ":() V 4:return public java.lang.String Plus (java.lang.string[]); code:0: Aconst_null 1:astore_2 2:iconst_0 3:istore_3 4:iload_3 5:aload_1 6 : Arraylength 7:if_icmpge Notoginseng 10:new #2//class Java/lang/stringbuilder 1 3:dup 14:invokespecial #3//Method java/lang/stringbuilder. " <init>:() V 17:aload_2 18:invokevirtual #4//Method java/lang/stringbuilder.append: (Lj ava/lang/string;) Ljava/lang/stringbuilder; 21:aload_1 22:iload_3 23:aaload 24:invokevirtual #4//Method Java/lang/stringbuilder. Append: (ljava/lang/string;) Ljava/lang/stringbuilder; 27:invokevirtual #5//Method java/lang/stringbuilder.tostring: () ljava/lang/string; 30:astore_2 31:iinc 3, 1 34:goto 4 37:aload_2 38:areTurn public java.lang.String Builder (java.lang.string[]); code:0: New #2//class Java/lang/stringbuilder 3:dup 4:invokespecial #3 Method Java/lang/stringbuilder. " <init> ":() V 7:astore_2 8:iconst_0 9:istore_3 10:iload_3 11:aload_1 12:arrayle Ngth 13:if_icmpge 16:aload_2 17:aload_1 18:iload_3 19:aaload 20:invokevirtual # 4//Method java/lang/stringbuilder.append: (ljava/lang/string;) Ljava/lang/stringbuilder; 23:pop 24:iinc 3, 1 27:goto 30:aload_2 31:invokevirtual #5 Method java/lang/stringbuilder.tostring: () ljava/lang/string; 34:areturn}
There is no discovery, there is a default null argument for the constructor test ();
Plus method, the StringBuilder Goto is the initialization of I, and then continue to run, compare, keep jumping, the loop in the continuous construction. So there are a lot of StringBuilder objects wasted. Note 10 there, the creation of new.
After the disassembly of the build method, there is no new thing in the loop, there is only one StringBuilder object.
After this analysis, it is obvious that Stringbuider is working on the string is more efficient resource-intensive, if it is simply a few string merges, it can use +.
This example uses the disassembly to look a lot simpler, in fact, some comparisons are through time calculation, of course, is also possible.
StringBuilder is introduced in the SE5 version, before using the StringBuffer, this question often something, appeared, StringBuffer is thread-safe, so the cost is larger than StringBuilder, so there is no thread problem, Use StringBuilder.
Can look at the following URL, which is known on a program Hunter wrote (by the chemical reading software, this is a very good choice). Inside is StringBuffer VS StringBuilder.
https://gist.github.com/programus/1978494
It turns out that GitHub had something to gist. A warehouse similar to storing fragmented code.
3) Common methods
A string implements three interfaces.
Charsequence:
charAt (int i) that returns the character that the index points to in the string.
Length, which returns string lengths.
subsequence (int start, int end), which returns the string that the index ends from start of start.
ToString ().
Comparable:
CompareTo (T o), X.compareto (y), so compared.
Other commonly used methods to check the JDK files themselves, commonly used is also used memorized. The formatted output and regular expressions of the string are described next.
Java Programming Ideas (12)--strings (1)