Java包装类和Number类的学习

Byte类

包:java.lang

类的声明:public final class Byte extends Number implements Comparable<Byte>{}

公共访问权限,最终的不可继承,类名为Byte,继承了Number类,实现了Comparable<Byte>接口

常量:public static final byte MIN_VALUE = -128;

  public static final byte MAX_VALUE = 127;

公共访问权限,静态的,最终的不可修改的,byte类型的,标识符为MIN_VALUE或MAX_VALUE

1、toString(byte b)方法

public static String toString(byte b){
 return Integer.toString((int)b,10);
}

公共访问权限,静态的,返回类型为String,方法名为toString,传入参数为一个byte类型数据。

在方法体内部,调用Integer类的toString静态方法,传入两个值,一个是将byte类型数据强制转换为int类型数据,另一个为10表示这是一个10进制数。最后返回方法的结果给本toString()方法。

2、value0f(byte b)方法

public static Byte valueOf(byte b) {
       final int offset = 128;
       return ByteCache.cache[(int)b + offset];
}

公共访问权限,静态方法,返回类型为Byte,方法名为valueOf,传入参数为一个byte类型数据。

定义一个int类型的常量,名为offset,值为128.

3、parseByte(String s,int radix)方法

    public static byte parseByte(String s, int radix)
        throws NumberFormatException {
        int i = Integer.parseInt(s, radix);
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                "Value out of range. Value:"" + s + "" Radix:" + radix);
        return (byte)i;
    }

公共访问权限,静态方法,返回类型为byte,类名为parseByte,传入两个参数,其中一个为字符串类型,另一个为int类型,表示进制,抛出NumberFormatException错误。

方法体内,将字符串s和int类型的进制表示传递给Integer类的parseInt静态方法,声明一个int类型的变量i接收方法结果。

如果i不在byte值范围内,则抛出错误;否则,将i强制转换为byte类型返回。

4、parse(String s)

    public static byte parseByte(String s) throws NumberFormatException {
        return parseByte(s, 10);
    }

与3相似,不主动设置进制,默认为10进制。

5、valueOf(String s, int radix)

    public static Byte valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseByte(s, radix));
    }

 在3的基础上,将结果传递给valueOf()方法,转换成Byte类型。

6、valueOf(String s)

    public static Byte valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

在4的基础上,将结果传递给valueOf方法,转换成Byte类型。

 7、decode(String nm)

    public static Byte decode(String nm) throws NumberFormatException {
        int i = Integer.decode(nm);
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                    "Value " + i + " out of range from input " + nm);
        return valueOf((byte)i);
    }

将需要解码的字符串交给Integer类的decode静态方法,获得int类型的返回值,再将其先转换为byte,再转换为Byte类型,返回给方法。

8、private final byte value;

声明一个私有的,byte类型的常量,标识符为value,默认值为0;

9、Byte(byte value)构造方法

    public Byte(byte value) {
        this.value = value;
    }

将byte类型的参数赋值给本类的value变量。

10、Byte(String s)构造方法

    public Byte(String s) throws NumberFormatException {
        this.value = parseByte(s, 10);
    }

通过字符串设置value值

11、byteValue()方法

    public byte byteValue() {
        return value;
    }

获得value属性值,类型为byte。

12、shortValue()方法

    public short shortValue() {
        return (short)value;
    }

获得value属性值,类型强制转换为short

13、intValue()方法

    public int intValue() {
        return (int)value;
    }

获得value属性值,类型强制转换为int

14、floatValue()方法

doubleValue()方法

15、toString()方法

   public String toString() {
        return Integer.toString((int)value);
    }

返回value值的字符串类型数据

原文地址:https://www.cnblogs.com/blunFan/p/11689820.html