Java lang.Long.byteValue() method in Java with Examples


java.lang.Long.byteValue() is a built-in function in Java that returns the value of this Long as a byte.

Syntax:

public byte byteValue()
Parameters: The function does not accept any parameter.
Return :
This method returns the numeric value 
represented by this object after conversion to byte type.

Examples:

Input : 12
Output : 12

Input : 1023
Output : -1

The program below illustrates the java.lang.Long.byteValue() function:

Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates the use of
// Long.byteValue() function
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Long value = 1023l;
  
        // returns the value of Long as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
  
        // 2nd example
        value = 12l;
        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 of a negative number

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates the use of
// Long.byteValue() function
// negative number
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Long value = -1023l;
  
        // returns the value of Long as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
  
        // 2nd example
        value = -12l;
        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

// Java program that demonstrates the use of
// Long.byteValue() function
// decimal number
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Long value = 11.24;
  
        // returns the value of Long as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
    }
}

chevron_right


Output:

prog.java:13: error: incompatible types: double cannot be converted to Long
        Long value = 11.24;

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates the use of
// Long.byteValue() function
// string number
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Long value = "24";
  
        // returns the value of Long as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
    }
}

chevron_right


Output:

prog.java:13: error: incompatible types: String cannot be converted to Long
        Long value = "24";


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.



Improved By : shubham_singh