一、定义
编译期:检查是否有语法错误,如果没有就将其翻译成字节码文件。即.class文件。
运行期:java虚拟机分配内存,解释执行字节码文件。
二、举例
public class demo {
public static final int a = 10;
public static final int b = 10;
public static int c = 10;
public static int d = 10;
public static void main(String[] args) {
int num1 = a * b;
int num2 = c * d;
}
}
a、b值是在编译期计算的,c、d值则是在运行时计算,即:
public class demo {
public static final int a = 10;
public static final int b = 10;
public static int c = 10;
public static int d = 10;
public static void main(String[] args) {
int num1 = 100;
int num2 = c * d;
}
}
三、常见情况
1、方法重载(编译时多态):在编译时执行。
2、方法重写(运行时多态):在运行时执行。
3、泛型(类型检测): 发生在编译时。
4、注解:注解即有可能是运行时也有可能是编译时。
5、异常:异常即有可能是运行时异常,也有可能是编译时异常。
6、继承 :它是静态的,发生在编译时。
7、代理或者组合:它更加具有动态性和灵活性,发生在运行时。