Saturday, December 6, 2025
HomeLanguagesJavaFloat byteValue() method in Java with Examples

Float byteValue() method in Java with Examples

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

Syntax:

public byte byteValue()

Parameters: The function does not accept any parameter.

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

Examples:

Input : 12
Output : 12

Input : 1023
Output : -1

Below programs illustrates the java.lang.Float.byteValue() function:

Program 1:




// Program to illustrate the Float.byteValue() method
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Float value = 1023f;
  
        // Returns the value of Float as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
  
        // Another example
        value = 12f;
        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 for a negative number.




// Java code to illustrate java.lang.Float.byteValue() method
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        Float value = -1023f;
  
        // Returns the value of Float as a byte
        byte byteValue = value.byteValue();
        System.out.println("Byte Value of num = " + byteValue);
  
        // Another example
        value = -12f;
        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.




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


Output:

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

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#byteValue()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32427 POSTS0 COMMENTS
Milvus
103 POSTS0 COMMENTS
Nango Kala
6802 POSTS0 COMMENTS
Nicole Veronica
11944 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12012 POSTS0 COMMENTS
Shaida Kate Naidoo
6932 POSTS0 COMMENTS
Ted Musemwa
7185 POSTS0 COMMENTS
Thapelo Manthata
6881 POSTS0 COMMENTS
Umr Jansen
6867 POSTS0 COMMENTS