Long valueOf(String s, int radix)、Long valueOf(String s)区别于用法

本文详细介绍了Java中Long类的两个静态方法:valueOf(String, int)与valueOf(String)的使用方法及注意事项。前者用于根据指定的基数将字符串转换为Long对象,后者则默认以十进制形式进行转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Long valueOf(String s,int radix) 与 Long valueOf(String s)这两个重载方法,是
包装类Long 中的两个static 静态方法,基本的功能都是将参数String s 转换为对应的long型数(实际上是直接转换为Long 类型的数,然后因为自动拆箱的存在,可以直接当做long型数据来使用)。

具体的使用细则如下:
(1)valueOf(String s,int radix)
中文版的API中是下面这么说明的:
在这里插入图片描述
即:

public static Long valueOf(String s,
int radix)
throws NumberFormatException返回一个Long对象,该对象保存从指定的String String的值,并用第二个参数给出的基数进行解析。 第一个参数被解释为在第二个参数指定的基数中表示一个签名的long ,就好像参数被赋予parseLong(java.lang.String, int)方法一样。 结果是一个Long对象,表示由字符串指定的long值。
换句话说,该方法返回一个Long对象的值等于:
new Long(Long.parseLong(s, radix))
参数
s - 要解析的字符串
radix - 用于解释的基数 s
结果
一个保存由指定基数中的字符串参数表示的值的 Long对象。
异常
NumberFormatException - 如果 String不包含可解析的 long 。

英文版的API说明如下:
在这里插入图片描述

即:

public static Long valueOf(String s,
int radix)
throws NumberFormatException
Returns a Long object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed long in the radix specified by the second argument, exactly as if the arguments were given to the parseLong(java.lang.String, int) method. The result is a Long object that represents the long value specified by the string.
In other words, this method returns a Long object equal to the value of:
new Long(Long.parseLong(s, radix))
Parameters:
s - the string to be parsed
radix - the radix to be used in interpreting s
Returns:
a Long object holding the value represented by the string argument in the specified radix.
Throws:
NumberFormatException - If the String does not contain a parsable long.


(2)valueOf(String s)
中文版的API中是这么说明的:
在这里插入图片描述
即:

public static Long valueOf(String s)
throws NumberFormatException返回一个Long指定的Long的值的Long String 。
该参数被解释为表示一个有符号的十进制long ,正如参数被赋予parseLong(java.lang.String)方法一样。 结果是一个表示由字符串指定的整数值的Long对象。
换句话说,此方法返回一个Long对象的值等于:
new Long(Long.parseLong(s))
参数
s - 要解析的字符串。
结果
一个保存由string参数表示的值的 Long对象。
异常
NumberFormatException - 如果字符串不能被解析为 long 。

英文版的API如下:
在这里插入图片描述
即:

public static Long valueOf(String s)
throws NumberFormatException
Returns a Long object holding the value of the specified String. The argument is interpreted as representing a signed decimal long, exactly as if the argument were given to the parseLong(java.lang.String) method. The result is a Long object that represents the integer value specified by the string.
In other words, this method returns a Long object equal to the value of:
---->new Long(Long.parseLong(s))
Parameters:
s - the string to be parsed.
Returns:
a Long object holding the value represented by the string argument.
Throws:
NumberFormatException - If the string cannot be parsed as a long.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值