|
Murat Kutluer wrote:
Any ideas why first one works but second one fails?
A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
Byte and the value of the constant expression is representable in the type byte. Short and the value of the constant expression is representable in the type short. Character and the value of the constant expression is representable in the type char.
Murat Kutluer wrote:Hi Daniel, there are compile errors on second and third lines. I checked it many times.
Daniel Cox wrote:I just noticed that my Eclipse compiler configuration for problems with boxing and unboxing conversions was set to "ignore"
A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
Byte and the value of the constant expression is representable in the type byte.
Short and the value of the constant expression is representable in the type short.
Character and the value of the constant expression is representable in the type char.
prateek shaw wrote:
3-As long as it is primitive type operation it is easy to understand and remember , example
prateek shaw wrote:
prateek shaw wrote:
I think on line no 7he mistakenly typed i instead of i1, where i1 is Integer i1= 100; If that is the case then although i1 is declared final It will not compile because can not cast from int to ByteHenry Wong wrote:
prateek shaw wrote:
Line 7 is actually not clear, as you did not show the declaration and initialization of the i variable. However, I will speculate that the variable is not a compile time constant, and hence, it is why line 7 does not compile. If the variable had been a compile time constant, then line 7 should compile (and work) fine.
Henry
I assume It is i1 not i then It will compile but will give runtime exception ClassCastException says Integer cannot be cast to Byte.prateek shaw wrote:
This line doesn't compile because the right hand side expression is not a compile time constant (as of the latest version of the JLS, wrapper classes are not treated as compile time constants). And since it is not a compile time constant, it can't use the special clause of section 5.2 to be implicitly narrowed from an int to a byte.
A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
Byte and the value of the constant expression is representable in the type byte.
Short and the value of the constant expression is representable in the type short.
Character and the value of the constant expression is representable in the type char.
francisco de feudis wrote:Is there any reason for this or should I learn it as a simple rule? Why the types Integer and Long are not allowed with narrowing + boxing conversion?
A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is:
Byte and the value of the constant expression is representable in the type byte.
Short and the value of the constant expression is representable in the type short.
Character and the value of the constant expression is representable in the type char.
francisco de feudis wrote:I don't understand why only Byte, Short and Character are allowed to use narrowing + autoboxing, why Long and Integer are excluded.
Integer I = (int)(byte) 1; //compile
francisco de feudis wrote:I wonder at this point why the compiler can't do this:
Integer I = (int)(byte) 1; //compile
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|