Wednesday, 11 December 2019

java - why when final variable initialize value inline that variable compatible to smaller data type without cast?

This is my Example java code.



class Example{
public static void main(String args[]){
int x = 65;
final int y = 65;
final int z;
z = 65;
char ch;

ch = 'A';
ch = 65;
ch =(char) x; // narrow casting
ch = y; //line 2
ch = z; //line 3 compile time error
}
}


why when final variable y initialize value inline that variable compatible to smaller data type without casting but final variable z initialize value after declared it that occurs compiler error.




Please tell me technically why this happens.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...