Java: Convert from Number to byte

Use Number.byteValue:

Number n = ...
byte b = n.byteValue()

Note that this simply discards all but the lowest 8 bits. If the Number is outside of the range −128…127 the conversion my have unexpected results.

Number byteValue()
100 100
127 127
128 −128
129 −127
256 0
1,000 −24
−1,000 24

Comments

Be the first to comment!