Why can't you use a double value for a Byte object?
Java will not implicitly narrow down a primitive value e.g. from double
to byte
so that you can explicitly pass it to byte
using a boxing transform. This protects against accidental loss of accuracy.
What you can do is explicitly point double
to byte
(lowercase); then Java implicitly inserts byte
into byte
. When you explicitly apply a primitive value for a narrower range type, you are telling the compiler, "Yes, I know I might lose precision, but I want this conversion anyway."
Byte r = (byte) x;
source to share