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.yiibai; 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