當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Byte parseByte()用法及代碼示例

用法:

    public static byte parseByte(String str);
    public static byte parseByte(String str, int radix's);

Short類parseByte()方法

  • parseByte() 方法可在java.lang包。
  • parseByte(String str) 方法用於返回與給定字符串表示對應的字節值,或者換句話說,我們可以說這個方法用於將字符串值轉換為字節值。
  • parseByte (String str, int radix's) 方法用於返回與給定字符串表示對應的字節值,作為第二個參數給出的基數中的有符號字節。
  • parseByte(String str), parseByte(String str, int radix's) 方法在從 String 轉換為 byte 時可能會拋出 NumberFormatException。NumberFormatException:在此異常中,如果 String 不包含可解析的數字。
  • 這些是靜態的方法,它們也可以通過類名訪問,如果我們嘗試使用類對象訪問這些方法,那麽我們也不會得到錯誤。

參數:

  • 在第一種情況下,String value– 表示 String 類型的值。
  • 在第二種情況下,String value, int radix's– 在這個方法中的第一個參數value表示 String 類型中的值radix's由第二個參數給出。

返回值:

在第一種情況下,該方法的返回類型是byte- 它返回此字符串參數的字節表示。

第二種情況,這個方法的返回類型是byte- 它以第二個參數給出的基數返回字符串參數的字節表示。

例:

// Java program to demonstrate the example 
// of parseByte() method of Byte class

public class ParseByteOfByteClass {
    public static void main(String[] args) {
        // Variables initialization
        String str1 = "100";
        String str2 = "67";
        int radix = 10;

        // Object initialization
        Byte b1 = new Byte(str2);

        // It convert string into byte by calling parseByte(str1) method
        // and store the result in another variable of byte type
        byte result = b1.parseByte(str1);

        // Display result
        System.out.println("b1.parseByte(str1):" + result);

        // It convert string into byte with radix 20 by calling parseByte(str1,radix's) method
        // and store the result in a variable of byte type
        result = b1.parseByte(str1, radix);

        // Display result
        System.out.println("b1.parseByte(str1,radix):" + result);
    }
}

輸出

b1.parseByte(str1):100
b1.parseByte(str1,radix):100


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java Byte class parseByte() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。