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.

### Java中因字符串'6f'导致的数字格式异常问题分析 当尝试将字符串`"6f"`转换为整数时,可能会抛出 `java.lang.NumberFormatException` 异常。这是因为默认情况下,`Integer.parseInt()` 方法假设输入是一个十进制数值[^2]。然而,`"6f"` 实际上是以十六进制表示的一个值。 #### 解决方案 为了正确解析 `"6f"` 这样的十六进制字符串并将其转换为整数,可以使用 `Integer.parseInt(String s, int radix)` 方法,并指定基数为 16: ```java String hexStr = "6f"; try { int decimalValue = Integer.parseInt(hexStr, 16); System.out.println("Decimal value of '6f': " + decimalValue); // 输出应为 111 } catch (NumberFormatException e) { System.err.println("Invalid hexadecimal string: " + e.getMessage()); } ``` 如果需要处理更大的范围(例如超出 `int` 的最大值),则可以改用 `Long.parseLong()` 或者直接通过 `BigInteger` 来实现更安全的操作: ```java import java.math.BigInteger; String hexStr = "6f"; try { BigInteger bigIntValue = new BigInteger(hexStr, 16); System.out.println("BigInteger value of '6f': " + bigIntValue.toString()); // 输出应为 111 } catch (NumberFormatException e) { System.err.println("Invalid hexadecimal string: " + e.getMessage()); } ``` #### 关于CA数字证书序列号的特殊处理 在处理 CA 数字证书中的序列号时,通常会遇到以十六进制形式存储的数据。这些数据可能被编码成字符串或其他格式,在程序中读取后需正确解码。以下是推荐的做法之一: 1. **验证输入合法性**:确保传入的字符串仅由合法字符组成(对于十六进制而言,允许的是 `[0-9a-fA-F]`)。可以通过正则表达式完成初步校验。 ```java public static boolean isValidHexadecimal(String str) { return str.matches("[0-9a-fA-F]+"); } ``` 2. **统一大小写**:某些场景下,十六进制串可能是混合大小写的(如 `6F` 和 `6f` 都代表相同值)。建议标准化为全小写或大写以便后续操作一致化。 3. **考虑边界条件**:注意检查是否为空白或者长度过短的情况;另外还需留意溢出风险——即该数值是否超出了目标型的承载能力(比如从 Hex 转 Int 可能失败因为原始值太大)。 以上方法能够有效应对由于错误解释非标准进位系统所引发的各种潜在陷阱[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值