使用包装器将字符串转换为数值?

huangapple 未分类评论41阅读模式
英文:

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;
    }

huangapple
  • 本文由 发表于 2020年7月26日 01:54:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/63091661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定