Java.lang.Byte.parseByte() 方法
描述
java.lang.Byte.parseByte(String s) 将字符串参数解析为带符号的十进制字节。 字符串中的字符必须都是十进制数字,除了第一个字符可以是 ASCII 减号 '−' ('\u002D') 表示负值或 ASCII 加号 '+' ('\u002B') 表示正值。
返回结果字节值,就像将参数和基数 10 作为参数提供给 parseByte(java.lang.String, int) 方法一样。
声明
以下是 java.lang.Byte.parseByte() 方法的声明。
public static byte parseByte(String s)throws NumberFormatException
参数
s − 一个包含要解析的字节表示的字符串
返回值
该方法返回十进制参数表示的字节值。
异常
NumberFormatException − 如果字符串不包含可解析的字节。
示例
下面的例子展示了 lang.Byte.parseByte() 方法的使用。
package com.tutorialspoint;
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

