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)