public class TestFinal {
public void show1( String str){
str+="c";
System.out.println(str);
}
public void show4( StringBuffer str){
str.append("nihao");
System.out.println(str);
}
public void show2( final String str){
// str=str+"!" 编译失败,final修饰的对象引用不可变
}
public void show3( final StringBuffer str){
//str=str.append("hi");//编译失败,final修饰的对象引用不可以变
str.append("hi"); //final修饰对象的具体内容可变
}
public static void main(String[] args) {
String str="hello word";
StringBuffer sb = new StringBuffer("hello word");
TestFinal tf =new TestFinal();
tf.show1(str); // 打印 hello wordc
System.out.println(str); //打印 hello word(局部变量,值传递,不会改变参数值)
System.out.println("**************************************************");
tf.show4(sb);//打印 hello wordnihao
System.out.println(sb);//打印 hello wordnihao (引用传递,方法更改了引用对象的值,则引用对象值也就变化了)
}
}
java中的final
最新推荐文章于 2022-03-27 21:48:42 发布