Java.lang.Byte.valueOf() Method


Description

The java.lang.Byte.valueOf(String s, int radix) returns a Byte object holding the value extracted from the specified String when parsed with the radix given by the second argument.

The first argument is interpreted as representing a signed byte in the radix specified by the second argument, exactly as if the argument were given to the parseByte(java.lang.String, int) method. The result is a Byte object that represents the byte value specified by the string.

Declaration

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

public static Byte valueOf(String s, int radix)throws NumberFormatException

Parameters

  • s − the string to be parsed

  • radix − the radix to be used in interpreting s

Return Value

This method returns a Byte object holding the value represented by the string argument in the specified radix.

Exception

NumberFormatException − If the String does not contain a parsable byte.

Example

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

package com.tutorialspoint;

import java.lang.*;

public class ByteDemo {

   public static void main(String[] args) {

      // create a String s and assign value to it
      String s = "-1010";

      // create a Byte object b
      Byte b;

      /**
       *  static method is called using class name.
       *  assign Byte instance value of s to b using radix as 2
       *  radix 2 represents binary
       */
      b = Byte.valueOf(s, 2);

      String str = "Byte value of string " + s + " using radix 2 is " + b;

      // print b value
      System.out.println( str );
   }
}

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

Byte value of string -1010 using radix 2 is -10
java_lang_byte.htm
Advertisements