![]() |
VOOZH | about |
The Double.byteValue() is a built-in method in Java Double class. This method converts the value of a Double object to a byte type. Basically, it is used for narrowing the primitive conversion of the Double type to a byte value.
In this article, we are going to learn about the Double.byteValue() method in Java with syntax and examples.
public byte byteValue()
Example 1: In this example, we are going to convert large positive numbers.
Byte value of 1023d: -1 Byte value of 12d: 12
Explanation: Here, the value 1023 exceeds the byte range, so the output wraps around to -1.
Example 2: In this example, we are going to see what happens with negative values.
Byte value of -1023d: 1 Byte value of -12d: -12
Explanation: Here, when -1023 is converted, it also wraps around due to overflow and produces an unexpected result i.e. 1.
Example 3: In this example, we are going to see what happens with decimal values.
Byte value of 11.24: 11 Byte value of 6.0: 6
Explanation: Here, the decimal part is truncated, not rounded and 11.24 becomes 11.
Example 4: In this example, we are going to see what happens when we do invalid conversion from string.
Output:
error: incompatible types: String cannot be converted to DoubleExplanation: We cannot assign a string directly to a Double variable.