Open In App

Java Double.byteValue() Method

Last Updated : 15 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Java
// Java program showing byteValue() with a large number
import java.lang.Double;

public class Geeks {
    
    public static void main(String[] args) {
        Double value = 1023d;

        byte b = value.byteValue();
        System.out.println("Byte value of 1023d: " + b);

        value = 12d;
        b = value.byteValue();
        System.out.println("Byte value of 12d: " + b);
    }
}

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.

Java
// Java program showing byteValue() with negative values
import java.lang.Double;

public class Geeks {
    
    public static void main(String[] args) {
        Double value = -1023d;

        byte b = value.byteValue();
        System.out.println("Byte value of -1023d: " + b);

        value = -12d;
        b = value.byteValue();
        System.out.println("Byte value of -12d: " + b);
    }
}

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.

Java
// Java program showing byteValue() with decimal values
import java.lang.Double;

public class Geeks {
    
    public static void main(String[] args) {
        Double value = 11.24;

        byte b = value.byteValue();
        System.out.println("Byte value of 11.24: " + b);

        value = 6.0;
        b = value.byteValue();
        System.out.println("Byte value of 6.0: " + b);
    }
}

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.

Java
// Invalid conversion from string
import java.lang.Double;

public class Geeks {
    
    public static void main(String[] args) {
        
        // cannot assign String to Double
        Double value = "45";

        byte b = value.byteValue();
        System.out.println("Byte value: " + b);
    }
}

Output:

error: incompatible types: String cannot be converted to Double

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


Next Article
Practice Tags :

Similar Reads