Java - Byte parseByte() 方法



描述

Java 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 − 如果字串不包含可解析的位元組。

將包含帶符號值的字串解析為位元組的示例

以下示例演示瞭如何使用 Byte parseByte() 方法解析帶有符號的字串。我們建立了兩個位元組變數和兩個包含有效值的字串變數。然後使用 parseByte() 方法獲取位元組,最後列印結果。

package com.tutorialspoint;
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

將包含無符號值的字串解析為位元組的示例

以下示例演示瞭如何使用 Byte parseByte() 方法解析不帶符號的字串。我們建立了兩個位元組變數和兩個包含有效值的字串變數。然後使用 parseByte() 方法獲取位元組,最後列印結果。

package com.tutorialspoint;
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 = "14";

      /**
       *  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 14 is 14

解析包含無效值的字串以獲取位元組時引發 NumberFormatException 的示例

以下示例演示瞭如何使用 Byte parseByte() 方法解析包含無效值的字串。我們建立了兩個位元組變數和兩個包含無效值的字串變數,因為值超出了範圍。然後使用 parseByte() 方法嘗試獲取位元組時,將丟擲 NumberFormatException。

package com.tutorialspoint;
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 = "1234";
      String s2 = "14";

      /**
       *  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 );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"1234" Radix:10
	at java.base/java.lang.Byte.parseByte(Byte.java:154)
	at java.base/java.lang.Byte.parseByte(Byte.java:178)
	at com.tutorialspoint.ByteDemo.main(ByteDemo.java:18)
java_lang_byte.htm
廣告