In integral data types where by default any literal is considered as int, if any literal is exceeding the intended data type range then we get "Possible loss of precision" error
for byte data type the the range of literals is -128 to 127
if we have byte b = 128;
compilation error: possible loss of precision
found : int
required : byte
now as per my understanding
we get this compilation error only if the literal is within the data types scope i.e. this error occurs in integral/floating data types if it is exceeding the range of the specified data type. if the literal is any other data type value then "incompatible types" error occurs.
Yes, the value 128 is outwith the range −128…127. So it cannot be a valid byte. The literal is always an int unless it ends with L. If you have a compile‑time constant which is within the range of a narrower datatype, the compiler will permit such assignment without a cast. Yes, I think you have got it right.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad: