Java - Number byteValue() Method



The Java Number byteValue() method converts the value of a specified number into its corresponding byte value. Here, we say a 'number' as it can be in any form: int, long, float, double.

Before we learn more about this method, let us first understand the concept of a byte.

A byte is defined as the unit of data that computers fundamentally use. This piece of data is eight binary digits (or bits) long and is used to represent various characters like numbers, letters or symbols. Some computers take 4 byte to constitute a word while some computers can take 2 bytes or one as well.

It is represented as ‘B’, and ranges from -128 to 127 (inclusive).

Note - This conversion may involve rounding or truncation of the given number.

Syntax

Following is the syntax for Java Number byteValue() method −

public byte byteValue()

Parameters

This method does not accept any parameters.

Return Value

This method returns the byte value after conversion.

Example

The following example shows the usage of Java Number byteValue() method. Here we have created an Integer object and then, tried to convert it into its byte value using the method.

public class NumberByteVal {
   public static void main(String[] args) {
   
      // Assign an input value to Integer class
      Integer x = new Integer(134);
	  
      // Print their value as byte
      System.out.println("x as integer: " + x + ", x as byte: " + x.byteValue());
	  
   }
 }

Let us compile and run the above program, this will produce the following result −

x as integer: 134, x as byte: -122

Example

In the example below, we are creating an object of the Long class and the byteValue() method is invoked on it. The long value will then be converted into its corresponding byte value.

public class NumberByteVal {

   public static void main(String[] args) {
   
      Long x = new Long(5632782);
      // Print their value as byte
      System.out.println("x as long: " + x + ", x as byte: " + x.byteValue());
   }
 }

On compiling and executing the program above, the output will be displayed as follows −

x as long: 5632782, x as byte: 14

Example

The following program assigns a float value to a Float class reference variable and invokes the given method on it to convert its float value into a byte value.

public class NumberByteVal {
   public static void main(String[] args) {
   
      Float x = 9876f;
      // Print their value as byte
      System.out.println("x as float: " + x  + ", x as byte: " + x.byteValue());
   }
}

After compiling and executing the program, the output is displayed as follows −

x as float: 9876.0, x as byte: -108

Example

The following example assigns a double value to a Double class reference and invokes the byteValue() method to convert the double value to its byte value.

public class NumberByteVal {
   public static void main(String[] args) {
   
      Double x = 3874d;
      // Print their value as byte
      System.out.println("x as double: " + x  + ", x as byte: " + x.byteValue());
   }
}

The output for the program above is as follows −

x as double: 3874.0, x as byte: 34
java_lang_number.htm
Advertisements