java final关键字

来源:互联网 发布:mac tomcat 编辑:程序博客网 时间:2024/05/29 09:56

1

public class Test {public static void main(String[] args) {final int num1[] = {1,2,3,4,5};num1[1] = 5;System.out.println(num1[1]);}}
报错代码:


public class Test {public static void main(String[] args) {final int num1[] = {1,2,3,4,5};int num2[] = {1,2,3,4,5,6};num1 = num2;}}
final int num1[]只是说你的num1数组不能再指向其他数组对象,
但它指向的数组的内容是可以变的!

java中的常量永远不能修饰对象的实际内容。
即:
final int b[2]={1,2}; 
b[1]++;                //OK                
b = new int(){1,2};    //NOT OK

int b[2]={1,2}; 
b[1]++;                //OK                
b = new int(){1,2};    //OK

原创粉丝点击