java long.valueof long.parselong 区别

个人理解:parseLong 做的主要功能是将字符串转换成long类型,但是valueof做了最后一步装包,而,平时使用
parseLong时,需要的也是Long类型,还会自动装包,所以这两者之间没有什么区别,唯一区别在:如果需要基本类型
时,那valuefo的动作就多余了两步(自动装包和自动拆包)

public static Long valueOf(String s) throws NumberFormatException
{
    return Long.valueOf(parseLong(s, 10));
}
public static Long valueOf(String s, int radix) throws NumberFormatException {
    return Long. valueOf( parseLong(s, radix));
}
=============================================================================================

public static long parseLong(String s)  throws NumberFormatException {
    return  parseLong(s,  10);
}
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;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
long.parseLongLong.valueOf都是用于将字符串转换为long类型的方法。它们的主要区别在于返回值的类型和异常处理方式。 long.parseLong方法的源代码如下: ```java public static long parseLong(String s) throws NumberFormatException { return parseLong(s, 10); } ``` 这个方法接受一个字符串参数,并将其解析为long类型的值。它默认使用十进制进行解析。如果字符串无法解析为有效的long值,将抛出NumberFormatException异常。 Long.valueOf方法的源代码如下: ```java public static Long valueOf(String s) throws NumberFormatException { return Long.valueOf(parseLong(s, 10)); } ``` 这个方法也接受一个字符串参数,并将其解析为Long对象。它内部调用了parseLong方法,并将其返回结果作为参数传递给Long.valueOf方法。这个方法的返回值是一个Long对象,而不是基本类型long。同样,如果字符串无法解析为有效的long值,将抛出NumberFormatException异常。 所以,long.parseLong方法返回的是基本类型long,而Long.valueOf方法返回的是Long对象。在使用时,可以根据具体需求选择使用哪个方法。 #### 引用[.reference_title] - *1* *2* *3* [Long.valueOf 与 Long.parseLong区别](https://blog.csdn.net/muyimo/article/details/122861947)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值