| /* final修饰局部变量:
 
 1.普通局部变量:其值不能更改;
 2.方法的形参,可以定义为final的:
 */
 class Demo
 {
 public static void main(String[] args)
 {
 int x = 10;
 show(x);
 
 int[] intArray = {1,3,2,43,4};
 show(intArray);
 System.out.println("intArray[0] : " + intArray[0]);
 }
 
 public static void show(){
 final int num = 10;
 //        num = 20;//编译错误。不能更改final变量的值;
 }
 
 public static void show(final int a){//形参是基本数据类型
 System.out.println("a = " + a);
 //        a = 20;//编译错误,不能更改形参a 的值;
 }
 public static void show(final int[] arr){//形参是引用数据类型
 arr[0] = 1000;//OK的,堆空间中的值是可以改变的
 arr = new int[2];//编译错误。一个final的引用,不能重新指向其它的堆空间。也就是其引用不能改变;
 
 }
 }
 
 |