Hi Madushika. Welcome to the Ranch!
The first example you give is explicitly allowed in the
Java Language Specification - see
here. It says:
In addition, if the expression is a constant expression (ยง15.28) of type byte, short, char, or int:
- A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
But you'll notice it doesn't mention
long, so that isn't allowed. Why? I can't tell, other than to say it's hard to think of a good reason why you'd need it.