Java编译时常量和运行时常量

Java编译时常量和运行时常量

编译期常量指的就是程序在编译时就能确定这个常量的具体值。

非编译期常量就是程序在运行时才能确定常量的值,因此也称为运行时常量。

在Java中,编译期常量指的是用final关键字修饰的基本类型或String类型并直接赋值(非复杂运算)的变量(无论是否用static修饰),是编译器的一种优化,体现在字节码文件中;运行是常量是由运行时解释器解释完成的

运行时常量很容易理解,接下来会简单的结合字节码文件来介绍编译时常量及使用风险。

public class ConstantTest {
    public final int a = 1;     //编译时常量
    public final int b = 1+2;   //编译时常量 支持加减乘除等简单运算
    public final int c = b+3;   //编译时常量 
    public final static int d = 10;   //编译时常量 
    public final String str1 = "abc"; //编译时常量 
    public final String str2 = "def" + "ghi";  //编译时常量 支持字符串的连接
    public final String str3 = str2 + "jkl";  //编译时常量 
    public final static String str4 = "static str";  //编译时常量 
    public final double e = Math.random(); //运行时常量
    public final ConstantTest test = new ConstantTest(); //运行时常量
}

下面是编译后相应的字节码信息:

 注意!!!!!!

类B中所有引用类A中用static final修饰的静态编译时常量都会在编译时全部替换为相应的数值或字符串,所以当修改了类A中的静态编译时常量时,类A和类B都要重新编译,如果只重新编译类A,则类B还是会引用之前的常量。

以下是案例:

类ConstantTest:

public class ConstantTest {
    public final int a = 1;     
    public final int b = 1+2;  
    public final int c = b+3;   
    public final static int d = 10;   
    public final String str1 = "abc"; 
    public final String str2 = "def" + "ghi";  
    public final String str3 = str2 + "jkl";  
    public final static String str4 = "static str";  
    public final double e = Math.random(); 
    public final ConstantTest test = new ConstantTest(); 
}

 类Test引用ConstantTest中的静态编译时常量:

扫描二维码关注公众号,回复: 8201390 查看本文章
public class Test{
    public int m = ConstantTest.d;
    public static int n = ConstantTest.d;
    public final int g = ConstantTest.d;
    public static final int j = ConstantTest.d;
    public static void main(String[] args) {
        int k = ConstantTest.d;
        System.out.println("m="+new Test().m);
        System.out.println("n="+n);
        System.out.println("g="+new Test().g);
        System.out.println("j="+j);
        System.out.println("k="+k);
    }
}

  首先全部编译,运行Test类:

   修改ConstantTest中静态常量的值为20后只重新编译ConstantTest,运行结果:

   重新编译Test后,运行结果:

  最后总结一下:

  1. 编译期常量指的是用final关键字修饰的基本类型或String类型并直接赋值(非复杂运算)的变量(无论是否用static修饰);
  2. 运行时常量是程序在运行时才能确定值的一种常量;
  3. 所有引用其他类中静态编译时常量(一般就叫静态常量,用static final修饰)的类在字节码中全部替换为相应常量的值,所以引用静态常量并不会触发该类的的初始化。

猜你喜欢

转载自www.cnblogs.com/xiaohang123/p/12046577.html