I have two programs which are almost identical. The first compiles, the second not. Why?
I am assigning an int
variable to a byte
variable.
This one compiles:
class Example {
public static void main(String args[]) {
final int x = 127; // directly initialized
byte b;
b = x;
System.out.println(b);
}
}
This one does not compile:
class Example {
public static void main(String args[]) {
final int x;
x = 127; // assigned later
byte b;
b = x;
System.out.println(b);
}
}
The compiler says incompatible types at b = x;
. But shouldn't the same apply to the first version as well?