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

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

以上仅是我个人的观点,


转载于:https://my.oschina.net/pumpkin0523/blog/194540

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值