java 编译时常量_java – 编译时常量和变量

Java语言文档说:

If a primitive type or a string is defined as a constant and the value

is known at compile time, the compiler replaces the constant name

everywhere in the code with its value. This is called a compile-time

constant.

我的理解是,如果我们有一段代码:

private final int x = 10;

然后,编译器将用文字10替换代码中每次出现的x.

但假设常量在运行时初始化:

private final int x = getX(); // here getX() returns an integer value at run-time.

与编译时常量相比,是否会出现性能下降(无论可能忽略不计)?

另一个问题是以下代码行:

private int y = 10; // here y is not final

编译器以与编译时常量相同的方式处理?

最后,我从答案中理解的是:

> final static表示编译时常量

> just final意味着它是一个常量但在运行时被初始化

>只是静态意味着在运行时初始化

>没有final是一个变量,不会被视为常量.

我的理解是否正确?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值