The byteValue() method of Integer class of java.lang package converts the given Integer into a byte after a narrowing primitive conversion and returns it (value of integer object as a byte). Also, remember this method does override byteValue() method of the Number class.
The package view is as follows:
--> java.lang Package --> Integer Class --> byteValue() Method
Syntax :
public byte byteValue()
Return Type: Returns the numeric value represented by this object after conversion to byte type.
Note: It is compatible with Java 1.5 and onwards.
Example 1:
Java
// Java program to Illustrate byteValue() Method // Of Integer Class // Importing required class import java.lang.Integer; // Main Class class GFG { // Main driver method public static void main(String args[]) { // Creating an Integer object and // passing custom integer input Integer a = new Integer( 34 ); // Converting integer number to byte value // using byteValue() method byte b = a.byteValue(); // Printing the corresponding byte value System.out.println(b); } } |
34
Example 2:
Java
// Java Program to Illustrate byteValue() Method // of Integer Class // Importing required classes import java.lang.*; import java.util.*; // Main Class class GFG { // Main driver method public static void main(String[] args) { // Creating a Byte object Byte b = new Byte( "01" ); // Converting Byte to byte primitive // using byteValue() method Byte bp = b.byteValue(); // Display statement System.out.println( "Byte object : " + b); // Primitive byte value of custom Byte passed above String str = "Primitive byte value of Byte object : " + bp; // Printing byte primitive value System.out.println(str); } } |
Byte object : 1 Primitive byte value of Byte object : 1