Java (string and StringBuffer are passed as parameters respectively)

Source: Internet
Author: User

Package com.day8.StringBuffer;


public class Demo7stringbuffer {

/**
* Common objects (string and StringBuffer are passed as parameters respectively)
* A: Formal parameter issues
* string is passed as a parameter
* StringBuffer as a parameter pass
* B: Case Demo
* String and StringBuffer are passed as parameters respectively.
* (the value of the base data type is passed without changing its value
* Value of the reference data type is passed, changing its value
* Although the string class is a reference data type, it is passed as a parameter when it is the same as the base data type
* StringBuffer is a reference data type that changes its value when passed as a parameter
*/
public static void Main (string[] args) {
String s= "Kobebryant";
Change (s);
System.out.println (s);//kobebryant, because the change () method is loaded into the stack memory, the stack is used up, so printing s or the original s
info (s);//kobebryantkg

StringBuffer sb=new stringbuffer ("Kobe");
Sb.append ("Bryant");
System.out.println (SB);//kobebryant
Change1 (SB);
System.out.println (SB);//kobebryantkg

}


public static void info (String s1) {
s1+= "KG";
System.out.println (S1);
}

public static void Change (String s) {
s+= "Itcast";
}

public static void Change1 (StringBuffer sb) {
sb+= "KG"; error, can't write like this
Sb.append ("KG");
}
}

Java (string and StringBuffer are passed as parameters respectively)

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.