java.lang.Integer byteValue()

Description :

This java tutorial shows how to use the byteValue() method of Integer class under java.lang package. This method returns the value of this Integer object as byte. This method override the byteValue() method of Number class.

Method Syntax :

public byte byteValue()

Parameter Input

DataType Parameter Description
N/A N/A N/A

Method Returns :

The byteValue() method simply returns the numeric value represented by this object after conversion to type byte.

Compatibility Version :

Requires Java 1.0 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of byteValue() method of Integer class. Basically it prints the equivalent in byte the value of this Integer Object.

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 Integer class.
 */

public class IntegerByteValue {

	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 the value from user console
		int intValue = scan.nextInt();		
		// close the scanner object
		scan.close();
		Integer myValue = new Integer(intValue);
		System.out.println("Byte Value is "+myValue.byteValue());

	}

}

Sample Output :

Running the toOctalString() method example source code of Integer class will give you the following output

Enter Desired Value:123
Byte Value is 123

Exception Scenario :

Exception in thread "main" java.util.InputMismatchException: For input string: "2147483648"
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at com.javatutorialhq.java.examples.IntegerToOctalString.main(IntegerToOctalString.java:21)

Similar Method :

  • N/A

Suggested Reading List :

References :