Double byteValue() Method in Java with Examples

The java.lang.Double.byteValue() is a built-in method in Java that returns the value of this Double as a byte(by casting to a byte). Basically it used for narrowing primitive conversion of Double type to a byte value.

Syntax:

public byte byteValue()

Parameters: The function does not accept any parameter.

Return Value: This method returns the double value represented by this object converted to type byte.

Examples:

Input : 12
Output : 12

Input : 1023
Output : -1

Below programs illustrates the use of java.lang.Double.byteValue() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the Double.byteValue() method
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Double value = 1023d;
  
        // Returns the value of Double as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
  
        // Another example
        value = 12d;
        byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
    }
}

chevron_right


Output:

Byte Value of num = -1
Byte Value of num = 12

Program 2 : Demonstrates the byte value for a negative number.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate java.lang.Double.byteValue() method
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Double value = -1023d;
  
        // Returns the value of Double as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
  
        // Another example
        value = -12d;
        byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
    }
}

chevron_right


Output:

Byte Value of num = 1
Byte Value of num = -12

Program 3 : When a decimal value is passed in argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate java.lang.Double.byteValue() method
  
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Double value = 11.24;
  
        // Returns the value of Double as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
  
        // Another example
        value = 6.0;
        byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
    }
}

chevron_right


Output:

Byte Value of num = 11
Byte Value of num = 6

Program 4 : When a string value is passed as argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Code to illustrate Double.byteValue()
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Double value = "45";
  
        // Returns the value of Double as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
    }
}

chevron_right


Compile Errors:

prog.java:9: error: incompatible types: String cannot be converted to Double
        Double value = "45";
                       ^
1 error

Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#byteValue–



My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.