Java - Byte valueOf() method



Description

The Java Byte valueOf(String s, int radix) method 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 1

The following example shows the usage of Byte valueOf(String,int) method. We've created a String variable and assigned it a value. Then a Byte variable is created and using Byte.valueOf(String,, int) method, the value of the string is parsed using radix as 2 and printed.

package com.tutorialspoint;
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 );
   }
}

Output

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

Byte value of string -1010 using radix 2 is -10

Example 2

The following example shows the usage of Byte valueOf(String,int) method. We've created a String variable and assigned it a value. Then a Byte variable is created and using Byte.valueOf(String,, int) method, the value of the string is parsed using radix as 8 and printed.

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

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

      // 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 8
       *  radix 8 represents octal
       */
      b = Byte.valueOf(s, 8);

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

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

Output

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

Byte value of string -101 using radix 8 is -65

Example 3

The following example shows the usage of Byte valueOf(String,int) method. We've created a String variable and assigned it a value. Then a Byte variable is created and using Byte.valueOf(String,, int) method, the value of the string is parsed using radix as 10 and printed.

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

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

      // 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 10
       *  radix 8 represents decimal
       */
      b = Byte.valueOf(s, 10);

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

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

Output

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

Byte value of string -101 using radix 10 is -101
java_lang_boolean.htm
Advertisements