I have tried another one
class wrap
{
public static void main(
String args[])
{
final int j =127;
byte b = j;
System.out.println(b);
}
}
It compiled .Then I tried this
class wrap
{
public static void main(String args[])
{
final int j =128;
byte b = j;
System.out.println(b);
}
}
This gives error
F:\javasid>javac wrap.java
wrap.java:6: Incompatible type for declaration. Explicit cast needed to convert
int to byte.
byte b = j;
^
1 error
Explanation:
When you chose the varibale to be final.Java stores it in smallest possible container.So,When you assign integer a value in the range of byte.Internally it stores only on single byte.
As soon as you go for a number that can't be stored it gives error.
It was a nice experiment.So your question gave opportunity to explore undocumented logic of final!!!
Congrats