VOOZH about

URL: https://www.geeksforgeeks.org/java/java-double-bytevalue-method/

⇱ Java Double.byteValue() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Double.byteValue() Method

Last Updated : 15 May, 2025

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.

Syntax of byteValue() Method

public byte byteValue()

  • Parameters: The function does not accept any parameters. 
  • Return Value: This method returns the double value represented by this object converted to type byte.

Examples of Java Double.byteValue() Method

Example 1: In this example, we are going to convert large positive numbers.


Output
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.


Output
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.


Output
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 Double

Explanation: We cannot assign a string directly to a Double variable.

Comment
Article Tags: