1. Interview question: What is the difference between string,stringbuffer,stringbuilder?
A: string is the content of the strings immutable, while StringBuffer and StringBuilder are variable length of the string content;
StringBuffer is synchronous, data security, low efficiency.
StringBuilder is not synchronized, the data is unsafe, and the efficiency is high.
2. Interview questions: StringBuffer and array differences?
A: Both can be seen as a container, loaded with other data. However, the StringBuffer data is ultimately a string data, and the array can place the same data of any type.
3. Interview questions: formal parameter issues.
String is passed as a parameter
StringBuffer passed as a parameter
Formal Parameters:
Basic type: Changes in formal parameters do not affect actual parameters
Reference type: Changes in formal parameters directly affect actual parameters
Case Demo:
Packagecn.itcast_08;/** Formal Parameter problem: * string passed as parameter * StringBuffer as parameter * * Formal parameter: * Basic type: Change of formal parameter does not affect actual parameter * Reference type: Change of formal parameter directly affects actual argument Number * * Note: * string is passed as a parameter, and the effect and the base type are passed as arguments. */ Public classStringbufferdemo { Public Static voidMain (string[] args) {String S1= "Hello"; String S2= "World"; System.out.println (S1+ "---" + s2);//Hello--- worldChange (s1, S2); System.out.println (S1+ "---" + s2);//Hello--- worldStringBuffer SB1=NewStringBuffer ("Hello"); StringBuffer SB2=NewStringBuffer ("World"); System.out.println (SB1+ "---" + sb2);//Hello--- worldChange (SB1, SB2); System.out.println (SB1+ "---" + sb2);//Hello---worldworld } Public Static voidChange (StringBuffer sb1, StringBuffer sb2) {SB1=SB2; Sb2.append (SB1); } Public Static voidChange (string s1, string s2) {S1=S2; S2= S1 +S2; }}
The results are as follows:
Java basic Knowledge Strengthening the 48:stringbuffer of the stringbuffer of the class three questions