Java基础(三)-final关键字分析

一 引言

  今天来谈谈final关键字的做用, 虽然有不少博文关于final进行了很深的研究,但仍是要去记录下谈谈本身的看法加深下印象。下面直接进入主题:html

二 final关键字的做用

  一、被final修饰的类不能被继承。java

  这一点应该不少人都知道也遇到过,经典案例就是java.lang.String类安全

  还有一些常见的类也是被final所修饰的,以下:ide

  基本类型对应的包装类型(如java.lang.Integer、java.lang.Long等)、字符相关类(java.lang.StringBuilder、java.lang.StringBuffer)、系统类(java.lang.Class、java.lang.System)等。就列这些其余就靠本身平时去发现。函数

  那么问题来了,a、为何final修饰的类不能被继承?答:这是Java语法定义的,无法。ui

  b、这样设计的目的是什么?答:由于类不须要被拓展类的、实现细节不容许改变,估计是为了安全考虑吧。this

  二、被final修饰的方法不能被重写spa

  其实这也是Java语法规定的,无法作解释。可是仔细回忆,这种状况跟static关键字修饰方法中一个特色相似,也是不能重写(覆盖)。设计

  下面咱们看案例(代码通过本身敲出来的才最有印象):code

class MyClass{
    final void test(){
        System.out.println("FinalClass");
    }
}
class MyClass2 extends MyClass {
    //编译报错:Cannot override the final method from MyClass
    public void test(){
        System.out.println("FinalClass"); }
}

    三、被final修饰的变量不能被“改变”

  先说下前提1:被final修饰的变量不像static那样。它也能够修饰局部变量。 

  前提2:被final修饰的变量必定要被初始化,不然编译不经过。

  针对前提,咱们先经过案例证实:

1 public class FinalTest {
2     //编译失败,不知足前提2。The blank final field count may not have been initialized
3     final int count;
4     public static void main(String[] args)  {
5         //编译经过。前提1:被final修饰的变量不像static那样。它也能够修饰局部变量。 
6         final int t = 0;
7     }
8 }

  初始化有两种:直接初始化和在构造函数中初始化(每一个构造函数都要初始化即每一个实例化对象的入口都要进行初始化)。

public class FinalTest {
    //直接初始化
    final int count = 0;
    final int num;
    //构造函数中初始化,若是没有对num进行初始化,就会编译错误。The blank final field num may not have been initialized
    public FinalTest(){
        num = 0;//注释这样就能够看到错误提示信息
    }
    public FinalTest(int t){
        num = 0;
        //this();//这两行左右开启同样才不会报错。
    }
}

  回归重点,被final修饰的变量,它是什么不能改变呢?变量值仍是变量的引用仍是二者都不能?看似有点玄乎(是否是本身有些没考虑到),其实也很简单(平时多留意就行)。依次举例证实:

    案例1(以基本类型为例):

 1 public class FinalTest {
 2     final int count = 0;
 3     
 4     public int getCount () {
 5         //The final field FinalTest.count cannot be assigned
 6         return count ++;
 7     }
 8     
 9     public static void main(String[] args)  {
10         FinalTest t = new FinalTest();
11         System.out.println(t.getCount());
12     }
13 }

  上面代码中第六行报错(The final field FinalTest.count cannot be assigned)了,因此能够得知:对于这种基本类型的变量被final所修饰后,它的值是不能被更改的。
  案例2(以对象为例):

 1 class Count {
 2     int count = 0;
 3     public int getCount () {
 4         return ++ count;
 5     }
 6 }
 7 
 8 public class FinalTest {
 9     
10     public static void main(String[] args)  {
11         final Count count1 = new Count();
12         final Count count2 = new Count();
13         System.out.println(count1.getCount());
14         System.out.println(count2.getCount());
15         //The final local variable count1 cannot be assigned. It must be blank and not using a compound assignment
16         count1 = count2;
17     }
18 }

  第16行一样的报错信息,可是这个就有点不同:对象里面的成员的值是能够改变的。因此针对这种对象变量而言,被final修饰后不可变的是变量的引用,而不是变量的内容。

  总结下这点:被final修饰的基本类型变量,它的值是不可变的。被final修饰的引用类型变量,它的引用地址是不可变的,对象里的内容是可变的。

 

三 final关键字的拓展

  一、在匿名类中使用外部内的变量,则该变量必须是final所修饰的。下面案例中第10就会编译报错,提示必须是final修饰的变量。

 1 public class FinalTest {
 2     
 3     public static void main(String[] args)  {
 4         int count = 0;
 5         
 6         Thread thread1 = new Thread(new Runnable() {
 7             @Override
 8             public void run() {
 9                 //Cannot refer to the non-final local variable count defined in an enclosing scope
10                 count ++;
11             }
12         });
13     }
14 }

  二、其实final还能够修饰形参。这样作的主要目的是防止无心的修改而影响到调用方法外的变量。若是你没了解这句就说明上面第三点做用你还没了解。

 1 class Count {
 2     int count = 0;
 3     public int getCount () {
 4         return ++ count;
 5     }
 6 }
 7 
 8 public class FinalTest {
 9     int num = 0;
10     public static void main(String[] args)  {
11         final Count count = new Count();
12         addCount(count);
13         System.out.println(count.count);
14     }
15     public static void addCount(final Count count){
16         count.getCount();
17         //count = new Count();//这种就是篡改。
18     }
19 }

 

  三、final变量与普通变量有什么区别,何时能够相等?看下下面代码,想下代码输出什么。

 1 public class FinalTest2 {
 2 
 3     public static void main(String[] args) {
 4         final String str1 = "test";
 5         final String str2 = getContent();
 6         String str3 = "test";
 7         
 8         String str4 = str1 + "";
 9         String str5 = str2 + "";
10         
11         System.out.println(str3 == str4);
12         System.out.println(str3 == str5);
13     }
14     public static String getContent(){
15         return "test";
16     }
17 }

  输出后的结果为true和false。这是为何呢?解释下你就清楚这二者的区别了。若是是final修饰直接定义的字符串或者是基本类型,它在编译期间就会肯定其值,则编译器会把它当作常量。因此当有使用到它的地方会直接用常量替换。而其余都是运行时才会肯定的值因此依然使用变量去计算。在代码中str2变量,虽然用是final修饰可是它的值要在的运行时才能肯定,因此它至关于普通变量。而str5这种计算方式并非咱们想象的简单,由于str2在这里成了普通变量,因此会经过stringBulider去计算整个表达式的值,因此返回也是一个新的str,引用地址变了。因此第12行的输出为false;

  四、final与finally 和finalize的区别

   finally是异常处理语句结构的一部分,表示最终执行。

finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,供垃圾收集时的其余资源回收,例如关闭文件等。

相关文章
相关标签/搜索