Integer.parseInt(String s) 和 Integer.valueOf(String s) 的区别
2023-03-07 09:42:20 时间
通过查看java.lang.Integer的源码可以发现, 它们最终调用的都是
/** * Parses the string argument as a signed integer in the radix * specified by the second argument. The characters in the string * must all be digits of the specified radix (as determined by * whether {@link java.lang.Character#digit(char, int)} returns a * nonnegative value), except that the first character may be an * ASCII minus sign {@code '-'} (<code>'\u002D'</code>) to * indicate a negative value or an ASCII plus sign {@code '+'} * (<code>'\u002B'</code>) to indicate a positive value. The * resulting integer value is returned. * * <p>An exception of type {@code NumberFormatException} is * thrown if any of the following situations occurs: * <ul> * <li>The first argument is {@code null} or is a string of * length zero. * * <li>The radix is either smaller than * {@link java.lang.Character#MIN_RADIX} or * larger than {@link java.lang.Character#MAX_RADIX}. * * <li>Any character of the string is not a digit of the specified * radix, except that the first character may be a minus sign * {@code '-'} (<code>'\u002D'</code>) or plus sign * {@code '+'} (<code>'\u002B'</code>) provided that the * string is longer than length 1. * * <li>The value represented by the string is not a value of type * {@code int}. * </ul> * * <p>Examples: * <blockquote><pre> * parseInt("0", 10) returns 0 * parseInt("473", 10) returns 473 * parseInt("+42", 10) returns 42 * parseInt("-0", 10) returns 0 * parseInt("-FF", 16) returns -255 * parseInt("1100110", 2) returns 102 * parseInt("2147483647", 10) returns 2147483647 * parseInt("-2147483648", 10) returns -2147483648 * parseInt("2147483648", 10) throws a NumberFormatException * parseInt("99", 8) throws a NumberFormatException * parseInt("Kona", 10) throws a NumberFormatException * parseInt("Kona", 27) returns 411787 * </pre></blockquote> * * @param s the {@code String} containing the integer * representation to be parsed * @param radix the radix to be used while parsing {@code s}. * @return the integer represented by the string argument in the * specified radix. * @exception NumberFormatException if the {@code String} * does not contain a parsable {@code int}. */ public static int parseInt(String s, int radix) throws NumberFormatException { }
这个parseInt是可以将字符串解析为各种进制的整数的, parseInt(String s)只是radix=10时的特例
而Integer.parseInt() 和 Integer.valueOf() 的区别主要在于放回的类型上, 一个是 int, 一个是Integer, 如果需要的是int, 性能上parseInt()会好点
public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); }
这里面调用的是 valueOf(int i) 方法, 其源码中为 -128 ~ 128 之间的Integer对象很贴心地做了缓存以提高效率
public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
相关文章
- STC8H开发(五): SPI驱动nRF24L01无线模块
- 联盛德 HLK-W806 (十三): 运行FatFs读写FAT和exFat格式的SD卡/TF卡
- STC8H开发(四): FwLib_STC8 封装库的介绍和注意事项
- STC8H开发(三): 基于FwLib_STC8的模数转换ADC介绍和演示用例说明
- STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解)
- STC8H开发(一): 在Keil5中配置和使用FwLib_STC8封装库(图文详解)
- STC8A,STC8G,STC8H系列的IRC内部振荡源频率调节
- STC8PROG - Linux下的 STC8A, STC8F, STC8G, STC8H 烧录工具
- 联盛德 HLK-W806 (十一): 软件SPI和硬件SPI驱动ST7567液晶LCD
- 老王电子的拆机 ESP32-SOLO-1 填坑报告
- 联盛德 HLK-W806 (十): 在 CDK IDE开发环境中使用WM-SDK-W806
- Thinkpad笔记本指点杆(小红点)自动漂移的问题
- 联盛德 HLK-W806 (九): 软件SPI和硬件SPI驱动ST7789V液晶LCD
- 离岸人民币和跨境人民币收款业务
- 清算(清分)与结算的区别
- Freaktab将于12月底关闭
- 联盛德 HLK-W806 (八): 4线SPI驱动SSD1306/SSD1315 128x64 OLED液晶屏
- 联盛德 HLK-W806 (七): 兼容开发板 LuatOS Air103
- STC12C5A56S2和DS12C887做的电子闹铃
- 嵌入式设备中按键的硬件消抖, 软件消抖和按键消息处理