请问 Long.parseLong 与 Long.valueOf有什么区别?

解决方案 »

  1.   

    关于这个问题,相信java的API文档是最权威也是最正确的 ^_^
    valueOf
    public static Long valueOf(String s,
                               int radix)
                        throws NumberFormatException当使用第二个参数给出的基数进行分析时,返回保持从指定 String 中提取的值的 Long 对象。第一个参数被解释为有符号的 long,基数由第二个参数指定,该值与用该参数作为参数的 parseLong(java.lang.String, int) 方法得到的值非常类似。结果是表示字符串指定的 long 值的 Long 对象。 
    换句话说,此方法返回一个 Long 对象,它的值等于: new Long(Long.parseLong(s, radix))参数:
    s - 要分析的字符串
    radix - 将在解释 s 时使用的基数 
    返回:
    保持由指定基数中的字符串参数表示的值的 Long 对象。 
    抛出: 
    NumberFormatException - 如果 String 不包含可分析的 long。
      

  2.   

    多看源码和api
    parseLong:
     /**
         * Parses the string argument as a signed decimal
         * <code>long</code>.  The characters in the string must all be
         * decimal digits, except that the first character may be an ASCII
         * minus sign <code>'-'</code> (<code>&#92;u002D'</code>) to
         * indicate a negative value. The resulting <code>long</code>
         * value is returned, exactly as if the argument and the radix
         * <code>10</code> were given as arguments to the {@link
         * #parseLong(java.lang.String, int)} method.
         * <p>
         * Note that neither the character <code>L</code>
         * (<code>'&#92;u004C'</code>) nor <code>l</code>
         * (<code>'&#92;u006C'</code>) is permitted to appear at the end
         * of the string as a type indicator, as would be permitted in
         * Java programming language source code.
         *
         * @param      s   a <code>String</code> containing the <code>long</code>
         *             representation to be parsed
         * @return     the <code>long</code> represented by the argument in 
         *    decimal.
         * @exception  NumberFormatException  if the string does not contain a
         *               parsable <code>long</code>.
         */
    Long.valueOf:
    /**
         * Returns a <code>Long</code> object holding the value
         * of the specified <code>String</code>. The argument is
         * interpreted as representing a signed decimal <code>long</code>,
         * exactly as if the argument were given to the {@link
         * #parseLong(java.lang.String)} method. The result is a
         * <code>Long</code> object that represents the integer value
         * specified by the string.
         * <p>
         * In other words, this method returns a <code>Long</code> object
         * equal to the value of:
         *
         * <blockquote><pre>
         * new Long(Long.parseLong(s))
         * </pre></blockquote>
         *
         * @param      s   the string to be parsed.
         * @return     a <code>Long</code> object holding the value
         *             represented by the string argument.
         * @exception  NumberFormatException  If the string cannot be parsed
         *              as a <code>long</code>.
         */
      

  3.   

    不好意思,刚发现贴错方法了。
    valueOf
    public static Long valueOf(String s)
                        throws NumberFormatException返回保持指定 String 的值的 Long 对象。该参数被解释为表示一个有符号的十进制 long,该值与用该参数作为参数的 parseLong(java.lang.String) 方法得到的值非常相似。结果是表示由字符串指定的整数值的 Long 对象。 
    换句话说,此方法返回一个 Long 对象,它的值等于: new Long(Long.parseLong(s))
     
    参数:
    s - 要分析的字符串。 
    返回:
    包含由字符串参数表示的值的 Long 对象。 
    抛出: 
    NumberFormatException - 如果不能将字符串分析为 long。
      

  4.   

    一样的,看源码:public static Long valueOf(String s, int radix) throws NumberFormatException {
    return new Long(parseLong(s, radix));
    }
      

  5.   

    之前我也看了源码,valueOf里只是调用了pareLong,没有什么不同啊,那为什么要两个方法?
      

  6.   

    说了半天都没看出区别,一个返回long是基本类型,一个返回Long是对象。能一样吗?
      

  7.   

    Long.parseLong()就是把传入的其他类型如String,转化为一个long的基本类型,  Long.valueOf就是把把传入的其他类型如String,转化为一个long的包装对象,还有一个方法就是longValue(),就是把一个包装对象转变为一个基本类型,也就是说parseLong() 与 valueOf()+longValue()用法相同