Java.lang.Byte.decode() Method



The java.lang.Byte.decode(String nm) decodes a String into a Byte. Accepts decimal, hexadecimal, and octal numbers given by the following grammar:

Decodable String

  • Signopt DecimalNumeral

  • Signopt 0x HexDigits

  • Signopt 0X HexDigits

  • Signopt # HexDigits

  • Signopt 0 OctalDigits


  • +

The sequence of characters following an optional sign and/or radix specifier ("0x", "0X", "#", or leading zero) is parsed as by the Byte.parseByte method with the indicated radix (10, 16, or 8).

This sequence of characters must represent a positive value or a NumberFormatException will be thrown. The result is negated if first character of the specified String is the minus sign. No whitespace characters are permitted in the String.


Following is the declaration for java.lang.Byte.decode() method

public static Byte decode(String nm)throws NumberFormatException


  • nm - the String to decode

Return Value

This method returns a Byte object holding the byte value represented by nm.


  • NumberFormatException - if the String does not contain a parsable byte


The following example shows the usage of lang.Byte.decode() method.

package com.tutorialspoint;

import java.lang.*;

public class ByteDemo {

   public static void main(String[] args) {

      // create 4 Byte objects
      Byte b1, b2, b3, b4;

       *  static methods are called using class name. 
       *  decimal value is decoded and assigned to Byte object b1
      b1 = Byte.decode("100");

      // hexadecimal values are decoded and assigned to Byte objects b2, b3
      b2 = Byte.decode("0x6b");
      b3 = Byte.decode("-#4c");

      // octal value is decoded and assigned to Byte object b4
      b4 = Byte.decode("0127");

      String str1 = "Byte value of decimal 100 is " + b1;
      String str2 = "Byte value of hexadecimal 6b is " + b2;
      String str3 = "Byte value of hexadecimal -4c is " + b3;
      String str4 = "Byte value of octal 127 is " + b4;

      // print b1, b2, b3, b4 values
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
      System.out.println( str4 );

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

Byte value of decimal 100 is 100
Byte value of hexadecimal 6b is 107
Byte value of hexadecimal -4c is -76
Byte value of octal 127 is 87