英文:
Wrapper Use Strings to Numeric?
问题
new Byte(string).byteValue():这行代码创建了一个新的Byte对象,然后通过byteValue()方法获取该对象的字节值。
请解释第二行与第一行有何不同。
英文:
Byte.parseByte(string)
new Byte(string).byteValue()
Please explain 2nd line how it is different from line 1.
答案1
得分: 0
parseByte方法返回一个原始类型的字节值。以下是该方法的代码:
public static byte parseByte(String s) throws NumberFormatException {
return parseByte(s, 10);
}
byteValue方法也返回一个字节值,但该字节值实际上是已创建的Byte对象的属性。请查看下面的源代码,其中Byte对象包含了一个byte类型的属性。
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
public byte byteValue() {
return value;
}
英文:
parseByte method returns a byte value which is a primitive type. Here is the code for this method:
public static byte parseByte(String s) throws NumberFormatException {
return parseByte(s, 10);
}
byteValue also returns a byte value but that byte value is actually a property of the Byte object that has been created. Look at the source code below where Byte object contains a property of byte type.
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
public byte byteValue() {
return value;
}
专注分享java语言的经验与见解,让所有开发者获益!
评论