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

基础

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

常量

//-2^63
@Native public static final long MIN_VALUE = 0x8000000000000000L;
//2^63-1
@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
//位数
@Native public static final int SIZE = 64;
//字节数
public static final int BYTES = SIZE / Byte.SIZE;

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

//初始化是-128~127的Long对象
private static class LongCache {
    private LongCache(){}

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

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

方法

转成特定进制的字符串

//i:数值
//radix:需要转换的进制(2~36 不在此范围的按10进制处理)
public static String toString(long i, int radix) {}
//默认转成10进制,当i==MIN_VALUE 直接返回数值"-9223372036854775808"
public static String toString(long i) {}
//调用toString(long i)
public String toString() {}

按无符号数值转特定进制的字符串,BigInteger

//无符号数转字符串
//i:数值
//radix:需要转换的进制(2~36 不在此范围的按10进制处理)
public static String toUnsignedString(long i, int radix) {}
private static BigInteger toUnsignedBigInteger(long i) {}
//转化成16进制无符号字符串
public static String toHexString(long i) {}
//转化成10进制无符号字符串
public static String toUnsignedString(long i) {}
//转化成8进制无符号字符串
public static String toOctalString(long i) {}
//转化成2进制无符号字符串
public static String toBinaryString(long i) {}

字符串转long

//s:数值字符串
//radix:s字符串是几进制(radix不在2~36则抛异常)
public static long parseLong(String s, int radix) throws NumberFormatException{}
//字符串为10进制
public static long parseLong(String s) throws NumberFormatException {}
//无符号字符串转long(字符串第一位为“-”抛异常)
//radix:表示s字符串是几进制(radix不在2~36则抛异常)
public static long parseUnsignedLong(String s, int radix) throws NumberFormatException {}
//字符串为10进制
public static long parseUnsignedLong(String s) throws NumberFormatException {}

字符串,long转Long

//s:数字型字符串
//radix:表示s字符串是几进制(radix不在2~36则抛异常)
public static Long valueOf(String s, int radix) throws NumberFormatException {}
//默认为10进制
public static Long valueOf(String s) throws NumberFormatException{}
//其中-128~127的Long是程序启动时就已经保存到LongCache中,所以在这个范围的数据是直接取的LongCache中的值
public static Long valueOf(long l) {}
//nm:可以是010(8进制)、0x10(16进制)(#开头的在这个方法中也表示16进制)
public static Long decode(String nm) throws NumberFormatException {}

Long转基本数据类型

public byte byteValue() {}
public short shortValue() {}
public int intValue() {}
public long longValue() {}
public float floatValue() {}
public double doubleValue() {}

获取系统属性的值

//nm:系统属性的名称
//val:如果为空时的默认值
public static Long getLong(String nm, long val) {}
public static Long getLong(String nm, Long val) {}
//返回默认值为null
public static Long getLong(String nm) {}

比较及求和

//返回-1,1,0(-1表示y大,1表示x大)
public int compareTo(Long anotherLong) {}
//返回-1,1,0(-1表示y大,1表示x大)
public static int compare(long x, long y) {}
//比较xy无符号数的大小,返回-1,1,0(-1表示y大,1表示x大)
public static int compareUnsigned(long x, long y) {}
public static long sum(long a, long b) {}
public static long max(long a, long b) {}
public static long min(long a, long b) {}

无符号数运算

//将dividend和divisor转成无符号整数相除取整
public static long divideUnsigned(long dividend, long divisor) {}
//将dividend和divisor转成无符号整数相除取余
public static long remainderUnsigned(long dividend, long divisor) {}

位运算
同Integer

猜你喜欢

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