您的位置:首页 > 编程语言 > Java开发

Long基本类型的三个静态方法(java)

2014-01-20 00:00 323 查看
摘要: Long基本类型的三个静态方法(java)

----Long.valueOf(String s); Long.parseLong(String s);Long.getLong(String s);

今天写代码是需要将时间戳转换为我们能看懂的时间格式,比如“年-月-日 时:分:秒”格式,需要将字符串类型的时间戳字符串转换为Long类型的数据。

在查看Long类型时,myeclipse提示了我觉得语义上可以满足以上需求的静态方法,如下三个

Long.valueOf(String s);

Long.parseLong(String s);

Long.getLong(String s);

因为字符串转换为int类型时,我们都用integer.parseInt(String s),而Long.getLong(String s)又是干什么的呢?因此我找到java.lang.Long文件,从源代码中看个究竟

Long.parseLong(String s)方法:

public static long parseLong(String s, int radix)

throws NumberFormatException

{

if (s == null) {

throw new NumberFormatException("null");

}

if (radix < Character.MIN_RADIX) {

throw new NumberFormatException("radix " + radix +

" less than Character.MIN_RADIX");

}

if (radix > Character.MAX_RADIX) {

throw new NumberFormatException("radix " + radix +

" greater than Character.MAX_RADIX");

}

long result = 0;

boolean negative = false;

int i = 0, len = s.length();

long limit = -Long.MAX_VALUE;

long multmin;

int digit;

if (len > 0) {

char firstChar = s.charAt(0);

if (firstChar < '0') { // Possible leading "+" or "-"

if (firstChar == '-') {

negative = true;

limit = Long.MIN_VALUE;

} else if (firstChar != '+')

throw NumberFormatException.forInputString(s);

if (len == 1) // Cannot have lone "+" or "-"

throw NumberFormatException.forInputString(s);

i++;

}

multmin = limit / radix;

while (i < len) {

// Accumulating negatively avoids surprises near MAX_VALUE

digit = Character.digit(s.charAt(i++),radix);

if (digit < 0) {

throw NumberFormatException.forInputString(s);

}

if (result < multmin) {

throw NumberFormatException.forInputString(s);

}

result *= radix;

if (result < limit + digit) {

throw NumberFormatException.forInputString(s);

}

result -= digit;

}

} else {

throw NumberFormatException.forInputString(s);

}

return negative ? result : -result;

}

一个参数的Long.parseLong(String s):

public static long parseLong(String s) throws NumberFormatException {

return parseLong(s, 10);

}

从这两个重载方法可以看出,一个参数的Long.parseLong(String s)方法只是将radix默认为10进制然后调用public static long parseLong(String s, int radix)方法

暂且不管这个,我们看一下Long.valueOf(String s)

public static Long valueOf(String s) throws NumberFormatException

{

return Long.valueOf(parseLong(s, 10));

}

我们会发现他调用了 Long.valueOf(Long l)的重载方法,参数为long类型,返回值仍为long类型。在实质转换上仍然是调用的是parseLong(String s, int radix)方法

我们再看getLong(String nm)方法:

public static Long getLong(String nm) {

return getLong(nm, null);

}

上面的方法实际调用的是一下方法

public static Long getLong(String nm, Long val) {

String v = null;

try {

v = System.getProperty(nm);

} catch (IllegalArgumentException e) {

} catch (NullPointerException e) {

}

if (v != null) {

try {

return Long.decode(v);

} catch (NumberFormatException e) {

}

}

return val;

}

由于前面调用的时val ==null,我测试了一下System.getProperty(nm)返回的是null,因此如果我们调用getLong(String nm)方法,返回的仍然是null,因此getLong(String nm)方法不能把字符串转换成为long类型。

至于

public static Long getLong(String nm, long val) {

Long result = Long.getLong(nm, null);

return (result == null) ? Long.valueOf(val) : result;

}

我们必须添加参数val 因此没有实际意义

经过对源代码的查看,我们很自然的会选择Long.parseLong(String s)方法,而Long.getLong(String s)不能将字符串转换为long类型。

以上仅是我个人的观点,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Long 转换 String