- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- final int x = 5;
- //x = 6;//编译不通过
- System.out.println(x);
-
- final StringBuilder sb=new StringBuilder("Hello World");
- //sb = new StringBuilder("trtt");//编译不通过
- sb.append("1234");
- System.out.println(sb.toString());
- }
- }
复制代码 final修饰基本数据类型变量时,基本数据类型变量的值不能改变
final int x = 5;
x = 6;//编译不通过.不能改变x的值
final修饰引用变量时,引用所指向的对象不能改变,对象中的内容可以改变
final StringBuilder sb=new StringBuilder("Hello World");
sb = new StringBuilder("trtt");//编译不通过
sb.append("1234");//内容可以改变
|