String和StringBuffer分别作为参数传递
-
A:形式参数问题
- String作为参数传递
- StringBuffer作为参数传递
-
B:案例演示
- String和StringBuffer分别作为参数传递问题
基本数据类型的值传递,不改变其值
引用数据类型的值传递,改变其值
String类虽然是引用数据类型,但是他当作参数传递时和基本数据类型是一样的
- String和StringBuffer分别作为参数传递问题
package com.heima.stringbuffer;
public class Demo07_StringBuffer {
public static void main(String[] args) {
String s = "heima";
System.out.println(s);
//String这个类一旦被初始化就不会被改变
//change方法调用完之后就弹栈了,hemaiitcast就消失了
//所以打印的s还是heima
change(s);
System.out.println(s);
System.out.println("---------------------");
StringBuffer sb = new StringBuffer();
sb.append("heima");
System.out.println(sb);
change(sb);
System.out.println(sb);
}
public static void change(StringBuffer sb) {
sb.append("itcast");
}
public static void change(String s) {
s += "itcast";
}
}

本文详细探讨了在Java中,String和StringBuffer作为参数传递时的行为差异。通过具体案例展示了基本数据类型与引用数据类型在参数传递过程中的值变化特点,强调了String类的不可变性及其对参数传递的影响。
445

被折叠的 条评论
为什么被折叠?



