Java Byte decode() 方法

2025年3月20日 | 阅读时长1分钟

Java Byte 类的 decode() 方法可以将一个 String 解码为一个 Byte。它可以接受十进制、十六进制和八进制的数字。

语法

参数

参数 'nm' 代表要解码的 String。

抛出

decode() 方法会抛出

NumberFormatException - 如果 String 不包含可解析的字节。

返回值

此方法返回一个 Byte 对象,其中包含字符串 'nm' 的字节值。

示例 1

示例

编译并运行

输出

12

示例 2

示例

编译并运行

输出

Exception in thread "main" java.lang.NumberFormatException: For input string: "null"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.valueOf(Integer.java:740)
	at java.lang.Integer.decode(Integer.java:1197)
	at java.lang.Byte.decode(Byte.java:277)
	at com.TpointTech.JavaByteDecodeExample2.main(JavaByteDecodeExample2.java:6)

示例 3

示例

编译并运行

输出

Exception in thread "main" java.lang.NumberFormatException: For input string: "Byte.MIN_VALUE"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.valueOf(Integer.java:740)
	at java.lang.Integer.decode(Integer.java:1197)
	at java.lang.Byte.decode(Byte.java:277)
	at com.TpointTech.JavaByteDecodeExample3.main(JavaByteDecodeExample3.java:7)
 
下一个主题Java Byte