zoukankan      html  css  js  c++  java
  • 谈使用final关键字

    谈使用final关键字   使用final关键字修饰类、方法和变量的作用大家都知道。不做过多介绍了,google搜索一下很多的。
        在使用final修饰方法的时候,编译器会将被final修饰过的方法插入到调用者代码处,提高运行速度和效率,但被final修饰的方法体不能过大,编译器可能会放弃内联,但究竟多大的方法会放弃,我还没有做测试来计算过。

    本文是通过两个疑问来继续阐述的:
    1、使用final修饰方法会提高速度和效率吗;
    见下面的测试代码,我会执行五次:
    Java代码
    1. public class Test  
    2. {  
    3.     public static void getJava()  
    4.      {  
    5.          String str1 = "Java ";  
    6.          String str2 = "final ";  
    7.         for (int i = 0; i < 10000; i++)  
    8.          {  
    9.              str1 += str2;  
    10.          }  
    11.      }  
    12.     public static final void getJava_Final()  
    13.      {  
    14.          String str1 = "Java ";  
    15.          String str2 = "final ";  
    16.         for (int i = 0; i < 10000; i++)  
    17.          {  
    18.              str1 += str2;  
    19.          }  
    20.      }  
    21.     public static void main(String[] args)  
    22.      {  
    23.         long start = System.currentTimeMillis();  
    24.          getJava();  
    25.          System.out.println("调用不带final修饰的方法执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间");  
    26.          start = System.currentTimeMillis();  
    27.          String str1 = "Java ";  
    28.          String str2 = "final ";  
    29.         for (int i = 0; i < 10000; i++)  
    30.          {  
    31.              str1 += str2;  
    32.          }  
    33.          System.out.println("正常的执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间");  
    34.          start = System.currentTimeMillis();  
    35.          getJava_Final();  
    36.          System.out.println("调用final修饰的方法执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间");  
    37.      }  
    38. }  
    public class Test { public static void getJava() { String str1 = "Java "; String str2 = "final "; for (int i = 0; i < 10000; i++) { str1 += str2; } } public static final void getJava_Final() { String str1 = "Java "; String str2 = "final "; for (int i = 0; i < 10000; i++) { str1 += str2; } } public static void main(String[] args) { long start = System.currentTimeMillis(); getJava(); System.out.println("调用不带final修饰的方法执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间"); start = System.currentTimeMillis(); String str1 = "Java "; String str2 = "final "; for (int i = 0; i < 10000; i++) { str1 += str2; } System.out.println("正常的执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间"); start = System.currentTimeMillis(); getJava_Final(); System.out.println("调用final修饰的方法执行时间为:" + (System.currentTimeMillis() - start) + "毫秒时间"); } }
    结果为:
    第一次:
    调用不带final修饰的方法执行时间为:1732毫秒时间
    正常的执行时间为:1498毫秒时间
    调用final修饰的方法执行时间为:1593毫秒时间
    第二次:
    调用不带final修饰的方法执行时间为:1217毫秒时间
    正常的执行时间为:1031毫秒时间
    调用final修饰的方法执行时间为:1124毫秒时间
    第三次:
    调用不带final修饰的方法执行时间为:1154毫秒时间
    正常的执行时间为:1140毫秒时间
    调用final修饰的方法执行时间为:1202毫秒时间
    第四次:
    调用不带final修饰的方法执行时间为:1139毫秒时间
    正常的执行时间为:999毫秒时间
    调用final修饰的方法执行时间为:1092毫秒时间
    第五次:
    调用不带final修饰的方法执行时间为:1186毫秒时间
    正常的执行时间为:1030毫秒时间
    调用final修饰的方法执行时间为:1109毫秒时间

        由以上运行结果不难看出,执行最快的是“正常的执行”即代码直接编写,而使用final修饰的方法,不像有些书上或者文章上所说的那样,速度与效率与“正常的执行”无异,而是位于第二位,最差的是调用不加final修饰的方法。
    观点:加了比不加好一点。


    1、使用final修饰变量会让变量的值不能被改变吗;
    见代码:
    Java代码
    1. public class Final  
    2. {  
    3.     public static void main(String[] args)  
    4.      {  
    5.          Color.color[3] = "white";  
    6.         for (String color : Color.color)  
    7.              System.out.print(color+" ");  
    8.      }  
    9. }  
    10.   
    11. class Color  
    12. {  
    13.     public static final String[] color = { "red", "blue", "yellow", "black" };  
    14. }  
    public class Final { public static void main(String[] args) { Color.color[3] = "white"; for (String color : Color.color) System.out.print(color+" "); } } class Color { public static final String[] color = { "red", "blue", "yellow", "black" }; }
    执行结果:
    red blue yellow white
    看!,黑色变成了白色。
        在使用findbugs插件时,就会提示public static String[] color = { "red", "blue", "yellow", "black" };这行代码不安全,但加上final修饰,这行代码仍然是不安全的,因为final没有做到保证变量的值不会被修改!原因是:final关键字只能保证 变量本身不能被赋与新值,而不能保证变量的内部结构不被修改。例如在main方法有如下代码Color.color = new String[]{""};就会报错了。那可能有的同学就会问了,加上final关键字不能保证数组不会被外部修改,那有什么方法能够保证呢?答案就是降 低访问级别,把数组设为private。这样的话,就解决了数组在外部被修改的不安全性,但也产生了另一个问题,那就是这个数组要被外部使用的。
    解决这个问题见代码:
    Java代码
    1. import java.util.AbstractList;  
    2. import java.util.List;  
    3.   
    4. public class Final  
    5. {  
    6.     public static void main(String[] args)  
    7.      {  
    8.         for (String color : Color.color)  
    9.              System.out.print(color + " ");  
    10.          Color.color.set(3, "white");  
    11.      }  
    12. }  
    13.   
    14. class Color  
    15. {  
    16.     private static String[] _color = { "red", "blue", "yellow", "black" };  
    17.     public static List<String> color = new AbstractList<String>()  
    18.      {  
    19.         @Override  
    20.         public String get(int index)  
    21.          {  
    22.             return _color[index];  
    23.          }  
    24.         @Override  
    25.         public String set(int index, String value)  
    26.          {  
    27.             throw new RuntimeException("为了代码安全,不能修改数组");  
    28.          }  
    29.         @Override  
    30.         public int size()  
    31.          {  
    32.             return _color.length;  
    33.          }  
    34.      };  
    import java.util.AbstractList; import java.util.List; public class Final { public static void main(String[] args) { for (String color : Color.color) System.out.print(color + " "); Color.color.set(3, "white"); } } class Color { private static String[] _color = { "red", "blue", "yellow", "black" }; public static List<String> color = new AbstractList<String>() { @Override public String get(int index) { return _color[index]; } @Override public String set(int index, String value) { throw new RuntimeException("为了代码安全,不能修改数组"); } @Override public int size() { return _color.length; } };
    }这样就OK了,既保证了代码安全,又能让数组中的元素被访问了。
  • 相关阅读:
    EOJ 2743 Stock Exchange
    POJ-3468 A Simple Problem with Integers
    EOJ-1104 bitmap
    【转】旋转卡壳——凸多边形间对踵点对(定义)
    Ring 3层枚举进程的四种方法
    XX-Net项目,免费浏览谷歌的伟大项目
    浅析Java中的内存机制
    Ubuntu下eclipse中安装Scala插件
    注入(5)---导入表注入(HookINT)
    Linux下MySQL导入文件出错ERROR 1290 (HY000)
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400642.html
Copyright © 2011-2022 走看看