java.lang.Float byteValue()

Description

The byteValue() method of Float class is used to get the equivalent of this Float object in byte primitive. This method is a little bit tricky to use since the float primitive can handle more precision than byte value thus if we will use the this method we might lost the precision the original value has. Let’s say we have the following declaration Float value = new Float(1212.4847f);. Certainly the value 1212.4847f is still within range of Float but what will happen if we will convert it to byte using byteValue() method? I will be discussing this thoroughly later on this document.

Important Notes:

  • overrides byteValue in class Number.

Method Syntax

public byte byteValue()

Method Returns

The byteValue() method of Float class returns the float value represented by this object converted to type byte

Discussion

The byteValue method is used to transform this Float object to primitive type byte. This is basically the core functionality of this method. As we all know a float data type can have decimal digits, what will happen to that. For example if we have the following expression: Float value = new Float(12.75f);. What will happen is that the decimal digit will get truncated if we use the byteValue method. What we mean we say the decimal digit will get truncated is that the value returned will be be 12.

As we have discussed on the earlier section of this document is that the range of Float is far greater than the byte primitive type. The byte primitive only has range of  127. So what will happen if we breached that range say for example Float value = new Float(128f);? For better understanding the minimum value of byte is -128 and max value of 127. If we try to convert greater than 127, the value that will be given is with the formula – + . And if the result is still greater than 128, keep doing – + until the result is within range of byte. So let’say we have a value 128, using the formula i have indicated the result would be -128. So this is the behavior of byteValue() method. As for my own view, I am not sure why we have the need to have this method. I could not see any practical usage yet or maybe I have yet to encounter one.

Java Float byteValue() Example

Below is a simple java example on the usage of byteValue() method of Float class.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

import static java.lang.System.*;

/*
 * This example source code demonstrates the use of 
 * byteValue() method of Float class.
 */

public class FloatByteValueExample {

	public static void main(String[] args) {
		
		// Ask user input
		System.out.print("Enter Desired Value:");
		// declare the scanner object
		Scanner scan = new Scanner(System.in);
		// use scanner to get value from user console
		Float userInput = scan.nextFloat();
		// close the scanner object
		scan.close();		
		// convert the Float input to byte
		byte value = userInput.byteValue();
		out.println("Value in byte is " + value);
		
	}

}

Basically on the above example, we just ask for user input on the console and then we use the scanner object to get the float input. After that we assign the value to an Float wrapper class and then get the byte equivalent using the byteValue() method of Float class.

Sample Output

Below is the sample output when you run the above example. We have entered 3.12 on the console however the decimal parts are truncated when we convert the value to byte. This is an expected behavior of using byteValue() method.