Java 编译时和运行时的区别

一、定义

编译期:检查是否有语法错误,如果没有就将其翻译成字节码文件。即.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、代理或者组合:它更加具有动态性和灵活性,发生在运行时。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Letian

您的打赏是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值