I think Vinayagar is correct. In line one it knows at compile time that '\u0001' is small enough to fit into a byte. Line two is getting its value from the method parameter so the compiler doesn't know if the char size will fit into a byte or not so it complains.
So, the point here is that a char is smaller than a byte and short and any conversions from a byte or short to char is considered as narrowing and requires an explicit cast. Am I right guys??
Can anyone confirm this??
Greg L Tonn
Joined: Nov 27, 2006
Bytes are 8 bit Char and short are 16 bit.
any conversions from a byte or short to char is considered as narrowing and requires an explicit cast
You're right in the fact that narrowing requires an explicit cast. However, what your method is doing is returning a 16 bit char to an 8 bit byte. This is a narrowing and requires an explicit cast as you said.
The first method does not require this because the compiler can tell that the char you are returning is small enough to fit into a byte so the complier will do an implicit cast for you.
Joined: Apr 09, 2006
Sorry Jothi, I guess you have misunderstood. If a byte is wider than char, the compilation wont fail. Just because char is wider than byte, the compiler gives error saying a char cannot be fit into a byte. Thats why the final keyword is required to ensure that the value of the char is so small that it can be fit into a byte. So, char is wider than byte and short and any conversion from short or byte to char does not require explicit cast. I guess this helps.
I guess you are wrong. Don't go by the bit size. Any conversions from a byte or short to char requires an explicit cast.
Joined: Apr 09, 2006
Dear Jothi, Sorry I was wrong. Any conversion from char to byte or short require explicit cast. Any conversion from byte or short to char also require explicit cast as well. I can understand that conversion between short & char require explicit cast because both are of same bit size. But why conversion from byte to char also requires explicit cast even though the char is wider than byte. Somebody clarify this?
In m1() Java compiler knows the exact value of c1 which in no way going to change but not the same for m2(). When changed m1() to pass the value dynamically, it will throw the same error. So final value in the method... no error...
It will give compile time error both at line1 and line2. Here I wanted to say that if you give the final value within the method it will not give error but if you pass the final value it will. That's right Greg, run time knowledge of the value will throw an error...
i think, you all confused much, even me too after reading your posts.
"all conversions between char and the two integer types byte and short are considered narrowing conversions: the reason being that the conversions between the unsigned type char and the signed types byte or short can result in loss of information."..............from K&B 1.4
--- ---- AND
"Additionally, implicit narrowing primitive conversions on assignment can occur in cases where all of the following 3 conditions are fulfilled:
1-- the source is a constant expression of either byte, short, char, or int type
2--the destination type is either byte, short, or char type
3---the value of the source is determined to be in the range of the destination type at compile time"