Java8 基础数据类型包装类-Byte

基础

//final修饰不可更改,每次赋值都是新建类(其中-128~127是直接从ByteCache中获取的不是新建的,可以使用==比较是否相同,其他数值是通过new新建的,不能使用==比较相同,因为地址不同,需用equals比较)
public final class Byte extends Number implements Comparable<Byte> {}

常量

//多少位
public tatic final int SIZE = 8;
//字节数
public static final int BYTES = SIZE / Byte.SIZE;
public static final byte   MIN_VALUE = -128;
public static final byte   MAX_VALUE = 127;

继承
抽象类Number
获取包装类与基本类型之间的转换值,short、int、long、byte、float、double。
实现
Comparable 接口
实现compareTo(T o)方法
数值范围
最小值:-128
最大值:127
私有静态内部类

//初始化-128~127的Byte数据
private static class ByteCache {
    private ByteCache(){}

    static final Byte cache[] = new Byte[-(-128) + 127 + 1];

    static {
        for(int i = 0; i < cache.length; i++)
            cache[i] = new Byte((byte)(i - 128));
    }
}

方法

byte,Byte转String

//调用Integer.toString方法默认10进制
public String toString() {}
//调用Integer.toString方法默认10进制
public static String toString(byte b) {}

byte转Byte

//通过(int)b+128,直接从ByteCache中取值
public static Byte valueOf(byte b) {}

String转byte,Byte

//s:数字字符串
//radix:字符串的进制(radix不在2~36则抛异常)
//方法先调用Integer.parseInt(),在判断数字大小,满足条件进行强制类型转换
public static byte parseByte(String s, int radix)
        throws NumberFormatException {}
//默认字符串为10进制
public static byte parseByte(String s) throws NumberFormatException {}
//调用parseByte,在byte->Byte
public static Byte valueOf(String s, int radix)
        throws NumberFormatException {}
//默认字符串为10进制
public static Byte valueOf(String s) throws NumberFormatException {}
//调用Integer.decode(),在判断数字大小,满足条件进行强制类型转换
public static Byte decode(String nm) throws NumberFormatException {}

Byte转基本数据类型

public byte byteValue() {}
public short shortValue() {}
public int intValue() {}
public long longValue() {}
public float floatValue() {}
public double doubleValue() {}
//无符号x转int
public static int toUnsignedInt(byte x) {}
//无符号x转long
public static long toUnsignedLong(byte x) {}

比较

//返回类型为int,结果为x-y的值。与integer的compare不一样,integer返回-1,1,0
public int compareTo(Byte anotherByte) {}
//返回类型为int,结果为x-y的值。与integer的compare不一样,integer返回-1,1,0
public static int compare(byte x, byte y) {}

猜你喜欢

转载自blog.csdn.net/u012562117/article/details/79011932
今日推荐