I am currently preparing for the
SCJP exam. And I am having real confusing time understanding primitive casting !
The below I have taken from the
http://www.danchisholm.net/ class GFC100 {
public static void main(
String[] args) {
final short s1 = 1; // 1
final char c1 = 1; // 2
byte b1 = s1; // 3
byte b2 = c1; // 4
byte b3 = 1; // 5
byte b4 = 1L; // 6
byte b5 = 1.0; // 7
byte b6 = 1.0d; // 8
}}
Question is which will generate compile time error :Answer is 6,7,8
I cant understand how 3 and 4 will work.
I have read that char is treated separately when it comes to casting and it cannot be used with byte and short for narrowing or widening(but can be used with int,long,float ,double for widening)
4 is a narrowing expression short to byte is allowed without the cast?
Can someone help me undertand how the primitive casting really works?
Thanks for your time!