Java Float byteValue Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the byteValue() method of the Float class in Java. The byteValue() method is used to convert a Float object into an equivalent byte value. In other words, it returns the byte equivalent of a Float object after a narrowing primitive conversion.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117642{{"`Java Float byteValue Method`"}} java/classes_objects -.-> lab-117642{{"`Java Float byteValue Method`"}} java/class_methods -.-> lab-117642{{"`Java Float byteValue Method`"}} java/modifiers -.-> lab-117642{{"`Java Float byteValue Method`"}} java/oop -.-> lab-117642{{"`Java Float byteValue Method`"}} java/wrapper_classes -.-> lab-117642{{"`Java Float byteValue Method`"}} java/identifier -.-> lab-117642{{"`Java Float byteValue Method`"}} java/arrays -.-> lab-117642{{"`Java Float byteValue Method`"}} java/comments -.-> lab-117642{{"`Java Float byteValue Method`"}} java/data_types -.-> lab-117642{{"`Java Float byteValue Method`"}} java/operators -.-> lab-117642{{"`Java Float byteValue Method`"}} java/output -.-> lab-117642{{"`Java Float byteValue Method`"}} java/strings -.-> lab-117642{{"`Java Float byteValue Method`"}} java/variables -.-> lab-117642{{"`Java Float byteValue Method`"}} java/system_methods -.-> lab-117642{{"`Java Float byteValue Method`"}} end

Creating a Float object

First, we will create a Float object. This Float object will contain a floating-point value that we will use to convert into an equivalent byte value.

Float num = 78.93f;

In the above code, we have created a Float object named num and assigned it a value of 78.93f.

Converting float to byte using byteValue() method

Next, we will use the byteValue() method to convert the Float object num into an equivalent byte value.

byte result = num.byteValue();

The byteValue() method returns the byte equivalent of the Float object, and we have stored this value in a byte variable named result.

Printing the byte value

Now, we will print the result byte value using the System.out.println() method.

System.out.println("Byte value of " + num + " is " + result);

The above code uses string concatenation to format the output string. We have printed the original floating-point value and its equivalent byte value.

Compile and run the program

Save the code to a file named FloatByteValue.java. Then, compile and run the program using the following commands in the terminal:

$ javac FloatByteValue.java
$ java FloatByteValue

Here is the full code:

public class FloatByteValue {
    public static void main(String[] args) {

        // Creating a Float object
        Float num = 78.93f;

        // Converting float to byte using byteValue() method
        byte result = num.byteValue();

        // Printing the byte value
        System.out.println("Byte value of " + num + " is " + result);
    }
}

Output:

Byte value of 78.93 is 78

Summary

In this lab, we learned about the byteValue() method of the Float class in Java. We saw how to use this method to convert a Float object into an equivalent byte value. The method is used to perform a narrowing primitive conversion. We also saw a sample program that demonstrates the use of the byteValue() method.

Other Java Tutorials you may like