final修饰引用变量,引用不可变,值可改变

498 阅读1分钟

final 修饰修饰基本数据类型类型时,是值不可变;修饰变量时,是引用不可变,但值是可变。

public static void main(String[] args) {
	final  int AGE =10;//AGE值不能改变
	/*
    //会导致代码编译不通过
    //系统提示 
    //The final local variable str cannot be assigned. It must be blank and not using a compound assignment
	final String str = "Hello";
	str = "World!";
	*/
	final StringBuffer stringBuffer = new StringBuffer("hello");
	//stringBuffer的值是可变
	stringBuffer.append(" world!");
	System.out.println(stringBuffer.toString());
}