Compare following code
public class Cat{
public static void main(String args[]){
int x = 20;
byte y = 10;
y = x;
System.out.println(y);
}
}
Compilation Error
java:8: possible loss of precision
found : int
required: byte
y = x;
^
1 error
Above code is OK, because I have to do casting like this
y =(byte) x;
but look at this
public class Cat{
public static void main(String args[]){
final int x = 20;
byte y = 10;
y = x;
System.out.println(y);
}
}
This example prints 20 and no compilation error
I do not understand why it implicitly converts int to byte, when I use final keyword.
Thanks in advance.
public class Cat{
public static void main(String args[]){
int x = 20;
byte y = 10;
y = x;
System.out.println(y);
}
}
Compilation Error
java:8: possible loss of precision
found : int
required: byte
y = x;
^
1 error
Above code is OK, because I have to do casting like this
y =(byte) x;
but look at this
public class Cat{
public static void main(String args[]){
final int x = 20;
byte y = 10;
y = x;
System.out.println(y);
}
}
This example prints 20 and no compilation error
I do not understand why it implicitly converts int to byte, when I use final keyword.
Thanks in advance.