目录

static Byte valueOf(String s, int radix)

描述 (Description)

java.lang.Byte.valueOf(String s, int radix)返回一个Byte对象,该对象保存使用第二个参数给出的基数解析时从指定String中提取的值。

第一个参数被解释为表示由第二个参数指定的基数中的有符号字节,就好像该参数被赋予parseByte(java.lang.String,int)方法一样。 结果是一个Byte对象,表示字符串指定的字节值。

声明 (Declaration)

以下是java.lang.Byte.valueOf()方法的声明

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

参数 (Parameters)

  • s - 要解析的字符串

  • radix - 用于解释s的基数

返回值 (Return Value)

此方法返回一个Byte对象,该对象包含指定基数中字符串参数表示的值。

异常 (Exception)

NumberFormatException - 如果String不包含可解析的字节。

例子 (Example)

以下示例显示了lang.Byte.valueOf()方法的用法。

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

让我们编译并运行上面的程序,这将产生以下结果 -

Byte value of string -1010 using radix 2 is -10
↑回到顶部↑
WIKI教程 @2018