- Java.lang 包類
- Java.lang - 首頁
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包其他內容
- Java.lang - 介面
- Java.lang - 錯誤
- Java.lang - 異常
- Java.lang 包有用資源
- Java.lang - 有用資源
- Java.lang - 討論
Java - Byte valueOf() 方法
描述
Java Byte valueOf(String s) 方法返回一個 Byte 物件,該物件包含由指定字串給出的值。該引數被解釋為表示一個有符號十進位制位元組,就像將該引數傳遞給 parseByte(java.lang.String) 方法一樣。
結果是一個 Byte 物件,它表示字串指定的位元組值。
宣告
以下是 java.lang.Byte.valueOf() 方法的宣告
public static Byte valueOf(String s) throws NumberFormatException
引數
s - 要解析的字串
返回值
此方法返回一個 Byte 物件,該物件包含字串引數表示的值。
異常
NumberFormatException - 如果字串不包含可解析的位元組。
使用加號解析字串以獲取位元組示例
以下示例演示了 Byte valueOf(String) 方法的用法。我們建立了一個字串變數併為其分配了一個值。然後建立一個 Byte 變數,並使用 Byte.valueOf(String) 方法解析字串的值並打印出來。
package com.tutorialspoint;
public class ByteDemo {
public static void main(String[] args) {
// create a String s and assign value to it
String s = "+120";
// create a Byte object b
Byte b;
/**
* static method is called using class name.
* assign Byte instance value of s to b
*/
b = Byte.valueOf(s);
String str = "Byte value of string " + s + " is " + b;
// print b value
System.out.println( str );
}
}
輸出
讓我們編譯並執行上述程式,這將產生以下結果:
Byte value of string +120 is 120
使用減號解析字串以獲取位元組示例
以下示例演示了 Byte valueOf(String) 方法的用法。我們建立了一個字串變數併為其分配了一個負值。然後建立一個 Byte 變數,並使用 Byte.valueOf(String) 方法解析字串的值並打印出來。
package com.tutorialspoint;
public class ByteDemo {
public static void main(String[] args) {
// create a String s and assign value to it
String s = "-120";
// create a Byte object b
Byte b;
/**
* static method is called using class name.
* assign Byte instance value of s to b
*/
b = Byte.valueOf(s);
String str = "Byte value of string " + s + " is " + b;
// print b value
System.out.println( str );
}
}
輸出
讓我們編譯並執行上述程式,這將產生以下結果:
Byte value of string -120 is -120
解析字串以獲取位元組時檢查 NumberFormatException 示例
以下示例演示了 Byte valueOf(String) 方法的用法。我們建立了一個字串變數併為其分配了一個無效值。然後建立一個 Byte 變數,並使用 Byte.valueOf(String) 方法解析字串的值,並且正如預期的那樣,發生了 NumberFormatException。
package com.tutorialspoint;
public class ByteDemo {
public static void main(String[] args) {
// create a String s and assign value to it
String s = "-1204";
// create a Byte object b
Byte b;
/**
* static method is called using class name.
* assign Byte instance value of s to b
*/
b = Byte.valueOf(s);
String str = "Byte value of string " + s + " is " + b;
// print b value
System.out.println( str );
}
}
輸出
讓我們編譯並執行上述程式,這將產生以下結果:
Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"-1204" Radix:10 at java.base/java.lang.Byte.parseByte(Byte.java:154) at java.base/java.lang.Byte.valueOf(Byte.java:208) at java.base/java.lang.Byte.valueOf(Byte.java:234) at com.tutorialspoint.ByteDemo.main(ByteDemo.java:17)
java_lang_byte.htm
廣告