Java.lang.Byte.valueOf() 方法
描述
java.lang.Byte.valueOf(String s, int radix) 返回一个 Byte 对象,当使用第二个参数给出的基数进行解析时,该对象保存从指定 String 中提取的值。
第一个参数被解释为表示由第二个参数指定的基数中的一个有符号字节,就像将该参数提供给 parseByte(java.lang.String, int) 方法一样。 结果是一个 Byte 对象,它表示字符串指定的字节值。
声明
以下是 java.lang.Byte.valueOf() 方法的声明。
public static Byte valueOf(String s, int radix)throws NumberFormatException
参数
s − 要解析的字符串
基数 − 用于解释 s 的基数
返回值
此方法返回一个 Byte 对象,该对象包含指定基数中的字符串参数表示的值。
异常
NumberFormatException − 如果字符串不包含可解析的字节。
示例
下面的例子展示了 lang.Byte.valueOf() 方法的使用。
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 );
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Byte value of string -1010 using radix 2 is -10

