Java 源码 - Long


介绍

The Long class wraps a value of the primitive type long in an object.

示例

public class Test {
  public static void main(String[] args) {
    System.out.println(Long.toString(12L));
  }
}

源码

public final class Long extends Number implements Comparable {
  /**
   * -263.
   */
  @Native public static final long MIN_VALUE = 0x8000000000000000L;

  /**
   * 263-1.
   */
  @Native public static final long MAX_VALUE = 0x7fffffffffffffffL;

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

  /**
   * Returns a Long instance representing the specified long value.
   */
  public static Long valueOf(long l) {
    final int offset = 128;
    if (l >= -128 && l <= 127) {
      return LongCache.cache[(int)l + offset];
    }
    return new Long(l);
  }

  private final long value;

  public Long(long value) {
    this.value = value;
  }

  public static int hashCode(long value) {
    return (int)(value ^ (value >>> 32));
  }

  public boolean equals(Object obj) {
    if (obj instanceof Long) {
      return value == ((Long)obj).longValue();
    }
    return false;
  }
  
  public static int compare(long x, long y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1);
  }
}