Java源码阅读之Long

类定义

public final class Long extends Number implements Comparable<Long>{}

1.类定义为final,不能被继承
2.类继承自Number类,是数值型变量,可以通过调用longValue()等形式的方法返回对应的值
3.实现了Comparable接口,可以通过调用compare方法与另一个Long类型的对象进行比较

类属性

// 最大值
@Native public static final long MIN_VALUE = 0x8000000000000000L;

//最小值
@Native public static final long MAX_VALUE = 0x7fffffffffffffffL;

// 类型属性
public static final Class<Long>  TYPE = (Class<Long>) Class.getPrimitiveClass("long");

构造方法

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

// 默认以十进制返回
public Long(String s) throws NumberFormatException{
	this.value = parseLong(s, 10);
}

方法

public static String toString(long i, int radix){
	if(radix < Character.MIN_RADIX || radix > Character.MAX_RADIX){
		radix = 10;
	}
	if(radix == 10){
		return toString(i);
	}
	char buf[] = new char[65];
	int charPos = 64;
	boolean negative = (i < 0);
	if(!negative){
		// 因为负数的范围比正数大1,所以选择将正数转换为负数
		i = -i;
	}
	while(i <= -radix){
		buf[charPos--] = Integer.digits[(int)(-(i % radix))];
		i = i / radix;
	}
	buf[charPos] = Integer.digits[(int)(-i)];
	if(negative){
		buf[--charPos] = '-';
	}
	return new String(buf, charPos, (65-charPos));
}

// 返回一个无符号长整数
public static String toUnsignedString(int i, int radix){
	if(i >= 0){
		return toString(i, radix);
	}else{
		switch(radix){
			case 2:
				return toBinaryString(i);
			case 4:
				return toUnsignedString0(i, 2);
			case 8:
				return toOctalString(i);
			case 10:
				long quot = (i >>> 1) / 5;
				long rem = i - quot * 10;
				return toString(quot) + rem;
			case 16:
				return toHexString(i);
			case 32:
				return toUnsignedString0(i, 5);
			default:
				return toUnsignedBigInteger(i).toString(radix);
		}
	}
}

private static BigInteger toUnsignedBigInteger(long i){
	if(i >= 0L){
		return BigInteger.valueOf(i);
	}else{
		int upper = (int)(i >>> 32);
		int lower = (int) i;
		return (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32).
			add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
	}
	
}

// 转换为二进制
public static String toBinaryString(long i){
	return toUnsignedString0(i, 1);
}

private static String toUnsignedString0(long i, int shift){
	int mag = Long.SIZE - numberOfLeadingZeros(i);
	int cahrs = Math.max((mag + shift - 1) / shift, 1);
	char buf[] = new char[chars];

	formatUnsignedLong(i, shift, buf, 0, chars);
	return new String(buf, true);
}

public static int numberOfLeadingZeros(int i){
	if(i == 0){
		return 64;
	}
	int n = 1;
	int x = (int)(i >>> 32);
	if(x == 0){n += 32; x = (int)i;}
	if(x >>> 16 == 0){n += 16; i <<= 16;}
	if(x >>> 24 == 0){n += 8; i <<= 8;}
	if(x >>> 28 == 0){n += 4; i <<= 4;}
	if(x >>> 30 == 0){n += 2; i <<= 2;}
	n -= x >>> 31;
	return n;
}

static int formatUnsignedLong(long val, int shift, char[] buf, int offset, int len){
	int charPos = len;
	int radix = 1 << shift;
	int mask = radix - 1;
	do{
		buf[offset + --charPos] = Integer.digits[val & mask];
		val >>> shift;
	}while(val != 0 && charPos > 0);
	return charPos;
}

public static String toString(long i){
	if(i == Long.MIN_VALUE){
		return "-9223372036854775808";
	}
	int size = (i < 0L) ? stringSize(-i) + 1 : stringSize(i);
	char buf[] = new char[size];
	getChars(i,size, buf);
	return new String(buf, true);
}

public static long parseLong(String s, int radix){
	if(s == null){

	}
	if(radix < Character.MIN_RADIX){}
	if(radix > Character.MAX_RADIX){}
	long result = 0;
	int len = s.length();
	boolean negative = false;
	int i = 0;
	long limit = -Long.MAX_VALUE;
	long multmin;
	int digit;
	if(len > 0){
		char firstChar = s.charAt(0);
		if(firstChar < '0'){
			if(firstChar == '-'){
				negative = true;
				limit = Long.MIN_VALUE;
			}else if(firstChar != '+'){
			}
				
			if(len == 1){}
			i++;
		}
		multmin = limit / radix;
		while(i < len){
			digit = Character.digit(s.charAt(i++), radix);
			if(digit < 0){
			}
			if(result < multmin){
			}
			result *= radix;
			if(result < limit + digit){
			}
			result -= digit;
		}
	}else{
	}
	return negative ? result : -result;
}

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

// valueOf会先去缓存池中寻找,如果有就直接返回
// 如果没有则新建一个
public static Long valueOf(long i){
	final int offset = 128;
	if(l >= -128 && l <= 127){
		return LongCache.cache[(int)l+offset];
	}
	return new Long(i);
}

public static Long decode(String nm) throws NumberFormatException{
	// 默认进制
	int radix = 10;
	int index = 0;
	boolean negative = false;
	Long result;
	if(nm.length() == 0) {
		throw new NumberFormatException("Zero length string");
	}
	char firstChar = nm.charAt(0);
	if(firstChar == '-') {
		negative = true;
		index ++;
	}else if (firstChar == '+') {
		index++;
	}
	
	if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
		index += 2;
		radix = 16;
	}else if (nm.startsWith("#", index)) {
		index++;
		radix = 16;
	}else if(nm.startsWith("0", index) && nm.length() > 1 + index) {
		// 如果nm.length() = 1 + index 则说明这是一个十进制数
		index ++;
		radix = 8;
	}
	if (nm.startsWith("+", index) || nm.startsWith("-", index)) {
		throw new NumberFormatException("Sign character in wrong position");
	}
	
	try {
		result = Long.valueOf(nm.substring(index), radix);
		result = negative ? Long.valueOf(-result.longValue()) : result;
	}catch (NumberFormatException e) {
		String constant = negative ? ("-" + nm.substring(index)) :nm.substring(index);
		result = Long.valueOf(constant, radix);
	}
	return result;
}

缓存池

// 缓存池被设置为私有,不能创建缓存池的实例
// Long的缓存池只缓存-128--127共256个数
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);
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值