Java - Short byteValue() Method



The Java Short byteValue() method retrieves the byte value equivalent to the given short value after the conversion of higher data type into lower data type i.e. narrowing primitive conversion. The byteValue() method of class Number is overridden by the byteValue() method of class Short.

In general, a data unit with eight binary digits is known as a byte. Most computers represent a character, such as a letter, number, or typographic sign, using a unit called a byte. Each byte has the capacity to store a string of bits that must be combined into a single larger unit for application needs.

For instance, the letter 'x' is one byte or eight bits, and the word 'crown' is five bytes or 40 bits (5*8).

Syntax

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

public byte byteValue()   

Parameters

This method does not accept any parameter.

Return Value

This method returns the short value that this object's converted-to-byte representation of a short value represents.

Example

Following is an example to show the usage of Java Short byteValue() method −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      Short s = 798;
      
      // Returning the short value in byte
      short value = s.byteValue();
      System.out.println("The byte value is: " + value);
   }
}

Output

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

The byte value is: 30

Example

In the example given below a Short object is created with a negative value and byteValue() method is called on it −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      Short s = -76;
      
      // Returning the short value in byte
      short value = s.byteValue();
      System.out.println("The byte value is: " + value);
   }
}

Output

Following is an output of the above code −

The byte value is: -76

Example

In the example below two Short variables ‘s1’ and ‘s2’ are created. Then the MAX_VALUE and MIN_VALUE is assigned to these variables. Thereafter, we retrieve its corresponding byte value −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      Short s1 = Short.MAX_VALUE;
      Short s2 = Short.MIN_VALUE;
      
      // Returning the short value in byte
      short value1 = s1.byteValue();
      short value2 = s2.byteValue();       
	   System.out.println("The byte value is: " + value1);
      System.out.println("The byte value is: " + value2);
   }
}

Output

On executing the program above, the output is obtained as follows −

The byte value is: -1
The byte value is: 0

Example

The following is an example to display the equivalent byte value of a string value, which is assigned to the Short object ‘s’. The byteValue() method is then invoked on the Short object −

import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      Short s = "789";
      
      // Returning the short value in byte
      byte value1 = s.byteValue();
      System.out.println("The byte value is: " + value1);
   }
}

Exception

As we can see in the below output an error occurs while reverting the byte value of a string which requires to get converted into a short value but it cannot happen −

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
      Type mismatch: cannot convert from String to Short

      at ShortDemo.main(ShortDemo.java:5)
java_lang_short.htm
Advertisements