java - Why during autoboxing final long to Byte compilation error happens, but final int to Byte is ok? -


there no error during auto-boxing of constants int , short types byte, constant long type has error. why?

final int = 3; byte b = i; // no error  final short s = 3; byte b = s; // no error   final long l = 3; byte b = l; // error 

from jls sec 5.2, "assignment contexts" (emphasis mine):

in addition, if expression constant expression (§15.28) of type byte, short, char, or int:

  • a narrowing primitive conversion may used if type of variable byte, short, or char, , value of constant expression representable in type of variable.
  • a narrowing primitive conversion followed boxing conversion may used if type of variable is:

    • byte , value of constant expression representable in type byte.
    • ...

it's not allowed longs spec.

note second bullet point here says happens irrespective of boxing: assigning constant long expression byte variable fail:

// both compiler errors. byte primitive = 0l; byte wrapped = 0l; 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -