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 :
import java.lang.*;
public class GFG {
public static void main(String[] args)
{
Long value = 1023l;
byte byteValue = value.byteValue();
System.out.println( "Byte Value of num = " + byteValue);
value = 12l;
byteValue = value.byteValue();
System.out.println( "Byte Value of num = " + byteValue);
}
}
|
Output:
Byte Value of num = -1
Byte Value of num = 12
Program 2 : Demonstrates the byte value of a negative number
import java.lang.*;
public class GFG {
public static void main(String[] args)
{
Long value = -1023l;
byte byteValue = value.byteValue();
System.out.println( "Byte Value of num = " + byteValue);
value = -12l;
byteValue = value.byteValue();
System.out.println( "Byte Value of num = " + byteValue);
}
}
|
Output:
Byte Value of num = 1
Byte Value of num = -12
Program 3 : When a decimal value is passed in argument.
import java.lang.*;
public class GFG {
public static void main(String[] args)
{
Long value = 11.24 ;
byte byteValue = value.byteValue();
System.out.println( "Byte Value of num = " + byteValue);
}
}
|
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.
import java.lang.*;
public class GFG {
public static void main(String[] args)
{
Long value = "24" ;
byte byteValue = value.byteValue();
System.out.println( "Byte Value of num = " + byteValue);
}
}
|
Output:
prog.java:13: error: incompatible types: String cannot be converted to Long
Long value = "24";
Last Updated :
23 May, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...