Why does the Java compiler not understand this variable is always initialized?(为什么Java编译器不理解这个变量总是被初始化?)
问题描述
class Foo{
public static void main(String args[]){
final int x=101;
int y;
if(x>100){
y=-1;
}
System.out.println(y);
}
}
Java 编译器理解 if 语句的条件始终为真,因此 y 将始终被初始化.正如预期的那样,没有编译错误.
Java compiler understands the condition of the if statement is always true and therefore y will always be initialized. No compile error, as expected.
class Bar{
public static void main(String args[]){
final int x;
x=101;
int y;
if(x>100){
y=-1;
}
System.out.println(y);
}
}
但是当我将 x 的声明和初始化分成两行时,编译器似乎没有得到条件始终为真并且 y 将始终被初始化.
But when I break the declaration and initialization of x into two lines, the compiler does not seem to get that the condition is always true and y will always be initialized.
final int x;
x=101;
byte b;
b=x;
System.out.println(b);
同样的事情发生在这里,编译器给出了精度损失错误.
Same thing happens here and the compiler gives a loss of precision error.
final int x=101;
byte b;
b=x;
System.out.println(b);
再次,编译器可以理解 x 在 b 的范围内.
Again, the compiler can understand that x is inside the range of b.
推荐答案
这与编译器如何确定语句是否执行有关.它在 JLS #16 中定义:
It has to do with how the compiler determines if a statement will be executed or not. It is defined in the JLS #16:
当访问其值时,每个局部变量和每个空白最终字段都必须有一个明确分配的值.
Each local variable and every blank final field must have a definitely assigned value when any access of its value occurs.
在您的情况下,编译器无法确定 y
已明确分配并给您一个错误.这是因为它需要确定条件始终为真,并且只有在 if
中的条件是 常量表达式 时才有可能.
In your case, the compiler can't determine that y
has been definitely assigned and gives you an error. This is because it would need to determine that the condition is always true and that is only possible if the condition in the if
is a constant expression.
JLS #15.28 定义常量表达式:
编译时常量表达式是表示原始类型值或字符串的表达式,它不会突然完成并且仅使用以下内容组成:
A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:
- [...]
- 引用常量变量(第 4.12.4 节)的简单名称(第 6.5.6.1 节).
JLS #4.12.4 将常量变量定义为:
原始类型或 String 类型的变量,它是 final 并使用编译时常量表达式初始化,称为常量变量.
A variable of primitive type or type String, that is final and initialized with a compile-time constant expression, is called a constant variable.
在您的情况下, final int x = 101;
是一个常量变量,但 final int x;x = 101;
不是.
In your case, final int x = 101;
is a constant variable but final int x; x = 101;
is not.
这篇关于为什么Java编译器不理解这个变量总是被初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么Java编译器不理解这个变量总是被初始化?
基础教程推荐
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01