1. Execution efficiency of String, StringBuffer, StringBuilder
StringBuilder > StringBuffer > String
Of course this is relative, not necessarily in all cases such as:
String a = ' a ' + ' B ' + ' C ' is more efficient than stringbuffer buffer = new StringBuffer (). Append (' a '). Append (' B '). Append (' C ') high
Therefore, each of the three classes have pros and cons, according to their own needs to decide which
String is recommended when there are fewer strings or fewer changes
It is recommended to use StringBuilder when the string is larger or the amount of churn is large, and if you consider using threads, use StringBuffer
1. What is the output of the following code?
String a = "Hello2"; String b = "Hello" + 2; System.out.println ((A = = b));
The result of the output is: true "Hello" + 2 is implicitly converted to "Hello2" at compile time so the A variable and the B variable point to the same object
2. What is the output of the following code?
String a = "Hello2"; String b = "Hello"; String C = b + 2; System.out.println ((A = = c));
The result of the output is: false because of the existence of quotation marks, so the compile time does not treat it as a constant
3. What is the output of the following code?
String a = "Hello2"; Final String b = "Hello"; String C = b + 2; System.out.println ((A = = c));
The result of the output is true the final modified variable will replace the variable directly with the real value at compile time to get "" springmvc+mybatis+spring Integration
4. The following code outputs the result:
public class main { public Static void main (String[] args) { string a = "Hello2"; final string b = gethello (); string c = b + 2; system.out.println ((a == c)); } public static string gethello () { return "Hello"; }}
The result of the output is: false Although the variable is final decorated but it is assigned by means of a method and is not known at compile time what value the method will return
5. What is the output of the following code?
public class main { public Static void main (String[] args) { string a = "Hello"; string b = New string ("Hello"); string c = new string ("Hello"); string d = b.intern (); System.out.println (a==b); system.out.println (b==c); system.out.println (B==d); System.out.println (a==d); }}
The result of the output is:
False
False
False
True
This involves the use of the String.intern method, where the intern method looks for the same string in the run-time pool and returns a reference to that address if there is one, so a and D point to the same object
How many objects are created by 6.String str = new String ("abc")?
This question in a lot of books have said, such as "Java Programmer interview book", including many domestic large companies written test questions will encounter, most of the online circulation and some interview books are said to be 2 objects, this statement is one-sided.
Obviously, new is called only once, which means that only one object is created.
Here is a confusing place where the code is loaded and executed at the time the difference is loaded is really only new one object is created at the time of the execution of a string object
The interviewer is asking the question to be clear about "how many objects this code has created at the time of execution, or simply how many objects this paragraph involves"
Of course, if it's a written test, it's on the Internet. 2 objects an ABC an object content created and initialized by new is ABC
The differences between string, StringBuffer and StringBuilder in Java and the problems that often arise in interviews