static byte parseByte(String s)
描述 (Description)
java.lang.Byte.parseByte(String s)将字符串参数解析为带符号的十进制字节。 字符串中的字符必须都是十进制数字,除了第一个字符可以是ASCII减号' - '('\ u002D')表示负值或ASCII加号'+'('\ u002B')表示正值。
返回结果字节值,就像将参数和基数10作为parseByte(java.lang.String,int)方法的参数给出一样。
声明 (Declaration)
以下是java.lang.Byte.parseByte()方法的声明
public static byte parseByte(String s)throws NumberFormatException
参数 (Parameters)
s - 包含要解析的字节表示的String
返回值 (Return Value)
此方法返回由十进制参数表示的字节值。
异常 (Exception)
NumberFormatException - 如果字符串不包含可解析的字节。
例子 (Example)
以下示例显示了lang.Byte.parseByte()方法的用法。
package com.iowiki;
import java.lang.*;
public class ByteDemo {
public static void main(String[] args) {
// create 2 byte primitives bt1, bt2
byte bt1, bt2;
// create and assign values to String's s1, s2
String s1 = "+123";
String s2 = "-123";
/**
* static method is called using class name.
* assign parseByte result on s1, s2 to bt1, bt2
*/
bt1 = Byte.parseByte(s1);
bt2 = Byte.parseByte(s2);
String str1 = "Parse byte value of " + s1 + " is " + bt1;
String str2 = "Parse byte value of " + s2 + " is " + bt2;
// print bt1, bt2 values
System.out.println( str1 );
System.out.println( str2 );
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Parse byte value of +123 is 123
Parse byte value of -123 is -123