Long基本類型的三個靜態方法(java)

 今天寫代碼是須要將時間戳轉換爲咱們能看懂的時間格式,好比「年-月-日 時:分:秒」格式,須要將字符串類型的時間戳字符串轉換爲Long類型的數據。java

   在查看Long類型時,myeclipse提示了我以爲語義上能夠知足以上需求的靜態方法,以下三個git

      Long.valueOf(String s); less

       Long.parseLong(String s);eclipse

       Long.getLong(String s); 測試

    由於字符串轉換爲int類型時,咱們都用integer.parseInt(String s),而Long.getLong(String s)又是幹什麼的呢?所以我找到java.lang.Long文件,從源代碼中看個究竟spa

    Long.parseLong(String s)方法:code

    public static long parseLong(String s, int radix)orm

              throws NumberFormatExceptionip

    {字符串

        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類型。

以上僅是我我的的觀點,

相關文章
相關標籤/搜索