Java basic Knowledge Strengthening the 48:stringbuffer of the stringbuffer of the class three questions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.