In this Java tutorial, you will learn about Integer.byteValue() method, and learn how to use this method to find the byte value of given integer, with the help of examples.

Integer.byteValue()

Integer.byteValue() returns the value of this Integer as a byte after a narrowing primitive conversion.

If this integer is out of the range for a byte, the value is narrowed down to a byte.

For example, if integer is 270, its byte value is 0001 0000 1110. As byte can only accommodate eight bits, the last eight bits of the given integer is considered as result. In this case the byte value would be 0000 1110, which is 14.

Syntax

The syntax of byteValue() method is

Integer.byteValue()

Returns

The method returns value of type byte.

ADVERTISEMENT

Examples

1. byteValue() of an integer value 270

In this example, we will take an integer a with value of say 270, and find its byte value using Integer.byteValue() method.

Java Program

public class Example{
	public static void main(String[] args){
		Integer a = 270;
		byte b = a.byteValue();
		System.out.println("Byte value of integer " + a + " = " + b);
	}
}

Output

Byte value of integer 270 = 14

2. byteValue() of integer in the range (-128, 127 )

In this example, we will take an integer such that the value is within the range of byte. In such case, Integer.byteValue() would return the same integer value.

Java Program

public class Example{
	public static void main(String[] args){
		Integer a = 42;
		byte b = a.byteValue();
		System.out.println("Byte value of integer " + a + " = " + b);
	}
}

Output

Byte value of integer 42 = 42

Conclusion

In this Java Tutorial, we have learnt the syntax of Java Integer.byteValue() method, and also how to use this method with the help of Java example programs.