JDK1.8源码解读之Long

1,096 阅读4分钟

概述

Long类在对象中包装了原始类型long的值。

  • 类型为Long的对象包含一个类型为long的字段。
  • 此外,此类还提供了几种将long转换为long和将Long转换为long的方法

继承关系

public final class Long extends Number implements Comparable

继承自 Number 实现了 Comparable 接口

成员属性

@Native public static final long MIN_VALUE = 0x8000000000000000L;

long能表示的最小值

@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;

long能表示的最大值

public static final Class TYPE = (Class) Class.getPrimitiveClass("long");

返回Long表示的基本类型的名称。也就是返回“long”

@Native public static final int SIZE = 64;

long类型需要的位数,也就是64位。

public static final int BYTES = SIZE / Byte.SIZE;

一个long类型占据的字节数 也就是 8位

构造器

public Long(long value) {
    this.value = value;
}
public Long(String s) throws NumberFormatException {
    this.value = parseLong(s, 10);
}

关键方法

+ toUnsignedString()
public static String toUnsignedString(long i, int radix) { ... }
+ toUnsignedString()

转化为无符号的int,然后转化为String

public static String toUnsignedString(int i, int radix) {
    return Long.toUnsignedString(toUnsignedLong(i), radix);
}
+ toHexString()

转化为16进制

public static String toHexString(long i) {
    return toUnsignedString0(i, 4);
}

相应的 还有转化为八进制和二进制的。

public static String toOctalString(long i) {
    return toUnsignedString0(i, 3);
}
public static String toBinaryString(long i) {
    return toUnsignedString0(i, 1);
}
+ parseLong()

按照指定的进制,String转化为long

public static long parseLong(String s, int radix) throws NumberFormatException { .... }

还有一个重载方法,默认采用10进制

public static long parseLong(String s, int radix)
throws NumberFormatException
+ parseUnsignedLong()

采用指定的进制,转为无符号的long

public static long parseUnsignedLong(String s, int radix)
throws NumberFormatException { ... }

还有一个重载方法,采用默认的10进制

public static long parseUnsignedLong(String s) throws NumberFormatException {
    return parseUnsignedLong(s, 10);
}
+ valueOf()

把一个String 按照指定的进制转化为Long

public static Long valueOf(String s, int radix) throws NumberFormatException {
    return Long.valueOf(parseLong(s, radix));
}

重载方法,采用了默认的10进制

public static Long valueOf(String s) throws NumberFormatException
{
    return Long.valueOf(parseLong(s, 10));
}
+ decode()

把一个String(表示二进制、10进制、八进制、16进制的字符传)解码为一个Long。

public static Long decode(String nm) throws NumberFormatException { ... }
+ byteValue()

当前Long对象转化为byte

public byte byteValue() {
    return (byte)value;
}

此外还有一个类似方法: shortValue(),intValue(),longValue(),floatValue(),doubleValue()

+ equals()

和另一个对象比较,如果另一个对象是Long,且value和当前Long相等。则返回true,否则返回false。

public boolean equals(Object obj) {
    if (obj instanceof Long) {
        return value == ((Long)obj).longValue();
    }
    return false;
}
+ compareTo()

和另一个Long对比,具体规则见compare()

public int compareTo(Long anotherLong) {
    return compare(this.value, anotherLong.value);
}
+ compare()

两个long比较,前者大,返回1;后者大,返回-1;相等,返回0;

public static int compare(long x, long y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
+ divideUnsigned()

对于参数 a b: 如果b < 0 && a < b, 返回 0, 如果b < 0 && a > b 返回 1; 如果 a > 0 && b > 0 , 返回 a/b 如果 a < 0 && b > 0 , 转化为无符号的Biginteger再相除。

public static long divideUnsigned(long dividend, long divisor) { ... }
+ remainderUnsigned()

对于参数 a b, 如果 a > 0 && b > 0, 返回 a/b 如果 a < 0 || b < 0, 且 a < b; 返回 a; 如果 a < 0 || b < 0, 且 a > b; 将 a b 转化为无符号的BigInteger 然后相除。

public static long remainderUnsigned(long dividend, long divisor) { ... }
+ highestOneBit()

对应的二进制,最高一位代表的int值。

public static long highestOneBit(long i) { ... }
+ lowestOneBit()

对应的二进制,最低一位代表的int值。

public static long lowestOneBit(long i) { ... }
+ rotateLeft()

循环左移

public static long rotateLeft(long i, int distance) {
    return (i << distance) | (i >>> -distance);
}
+ rotateRight()

循环右移

public static long rotateRight(long i, int distance) {
    return (i >>> distance) | (i << -distance);
}
+ reverse()

倒序排列

public static long reverse(long i) { ... }
+ sum()

求和

public static long sum(long a, long b) {
    return a + b;
}
+ max()

求大

public static long max(long a, long b) {
    return Math.max(a, b);
}
+ min()

求小

public static long min(long a, long b) {
    return Math.min(a, b);
}

内部类

预定了[-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);
    }
}

要点分析

reverse操作

采用的是位移实现的,而且采用的是归并的思路。 首先2个一组倒序位移。 然后4个一组倒序位移。 然后8个一组倒序位移。共4组。 然后这4组倒序位移。 采用位移操作计算速度更快。

希望和大家多多交流


我16年毕业以后,做的是前端,目前打算深入学习java开发。内容有任何问题,欢迎各位小伙伴们指正,也希望小伙伴们给我点赞和关注,给我留言,一起交流讨论,共同进步。