Cli*_*ote 414 java language-features
这两种方法有什么区别?他们似乎做同样的事情对我来说(也无二parseFloat(),parseDouble(),parseLong()等等,他们如何不同Long.valueOf(string)?
编辑:此外,哪些是更好的,并按惯例更常使用?
Zac*_*ena 385
那么,API Integer.valueOf(String)确实可以说它String被解释为完全被解释为Integer.parseInt(String).但是,valueOf(String)返回一个对象,而返回一个原语.new Integer()parseInt(String)int
如果你想享受潜在的缓存优势Integer.valueOf(int),你也可以使用这个眼睛:
Integer k = Integer.valueOf(Integer.parseInt("123"))
Run Code Online (Sandbox Code Playgroud)
现在,如果你想要的是对象,而不是原始的,然后使用valueOf(String)可能比制作一个新的对象出更有吸引力parseInt(String),因为前者是始终存在跨Integer,Long,Double,等.
Joa*_*lva 35
Integer.valueOf(s)
Run Code Online (Sandbox Code Playgroud)
类似于
new Integer(Integer.parseInt(s))
Run Code Online (Sandbox Code Playgroud)
区别是valueOf()返回an Integer,并parseInt()返回一个int(基本类型).另请注意,valueOf()可以返回缓存的Integer实例,这会导致混淆的结果,其中==测试结果似乎是间歇性正确的.在自动装箱之前,方便性可能会有所不同,在java 1.5之后它并不重要.
而且,Integer.parseInt(s)也可以采用原始数据类型.
Pau*_*est 14
看看Java源代码:valueOf正在使用parseInt:
/**
* Parses the specified string as a signed decimal integer value.
*
* @param string
* the string representation of an integer value.
* @return an {@code Integer} instance containing the integer value
* represented by {@code string}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as an integer value.
* @see #parseInt(String)
*/
public static Integer valueOf(String string) throws NumberFormatException {
return valueOf(parseInt(string));
}
Run Code Online (Sandbox Code Playgroud)
parseInt 回报 int
/**
* Parses the specified string as a signed decimal integer value. The ASCII
* character \u002d ('-') is recognized as the minus sign.
*
* @param string
* the string representation of an integer value.
* @return the primitive integer value represented by {@code string}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as an integer value.
*/
public static int parseInt(String string) throws NumberFormatException {
return parseInt(string, 10);
}
Run Code Online (Sandbox Code Playgroud)
Integer.parseInt只能将int作为本机类型返回.
Integer.valueOf实际上可能需要分配一个Integer对象,除非该整数恰好是预分配的对象之一.这需要更多费用.
如果您只需要本机类型,请使用parseInt.如果需要对象,请使用valueOf.
此外,由于这种潜在的分配,自动装箱在各方面都不是好事.它可以减慢事情.
Integer.parseInt 仅接受 String 并返回原始整数类型 (int)。
\n\n public static int parseInt(String s) throws NumberFormatException {\n return parseInt(s,10);\n }\nRun Code Online (Sandbox Code Playgroud)\n\nIteger.valueOf 接受 int 和 String。\n如果 value 是 String,valueOf 使用 parseInt 将其转换为简单 int,如果输入小于 -128 或大于 127,则返回新的 Integer。\n如果输入在范围 (-128 - 127) 内,它始终返回 Integer 对象来自内部 IntegerCache。Integer 类维护一个内部静态 IntegerCache 类,该类充当缓存并保存从 -128 到 127 的整数对象,这就是为什么当我们尝试获取 127 的整数对象(例如)时,我们总是得到相同的对象。
\n\nIteger.valueOf(200)将给出从 200 开始的新整数。就像new Integer(200)\n与;Iteger.valueOf(127)相同Integer = 127
如果您不想将字符串转换为整数,请使用Iteger.valueOf.
如果您不想将 String 转换为简单的 int 使用Integer.parseInt. 它工作得更快。
public static Integer valueOf(int i) {\n if (i >= IntegerCache.low && i <= IntegerCache.high)\n return IntegerCache.cache[i + (-IntegerCache.low)];\n return new Integer(i);\n }\n\n public static Integer valueOf(String s) throws NumberFormatException {\n return Integer.valueOf(parseInt(s, 10));\n }\n\n private static class IntegerCache {\n static final int low = -128;\n static final int high;\n static final Integer cache[];\n\n static {\n // high value may be configured by property\n int h = 127;\n String integerCacheHighPropValue =\n sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");\n if (integerCacheHighPropValue != null) {\n try {\n int i = parseInt(integerCacheHighPropValue);\n i = Math.max(i, 127);\n // Maximum array size is Integer.MAX_VALUE\n h = Math.min(i, Integer.MAX_VALUE - (-low) -1);\n } catch( NumberFormatException nfe) {\n // If the property cannot be parsed into an int, ignore it.\n }\n }\n high = h;\n\n cache = new Integer[(high - low) + 1];\n int j = low;\n for(int k = 0; k < cache.length; k++)\n cache[k] = new Integer(j++);\n\n // range [-128, 127] must be interned (JLS7 5.1.7)\n assert IntegerCache.high >= 127;\n }\n\n private IntegerCache() {}\n }\nRun Code Online (Sandbox Code Playgroud)\n\n比较 Integer.valueOf(127) == Integer.valueOf(127) 返回 true
\n\nInteger a = 127; // Compiler converts this line to Integer a = Integer.valueOf(127);\nInteger b = 127; // Compiler converts this line to Integer b = Integer.valueOf(127);\na == b; // return true \nRun Code Online (Sandbox Code Playgroud)\n\n因为它从缓存中获取具有相同引用的 Integer 对象。
\n\n但是 Integer.valueOf(128) == Integer.valueOf(128) 为 false,因为 128 超出了 IntegerCache 范围,并且它返回 new Integer,因此对象将具有不同的引用。
\n| 归档时间: |
|
| 查看次数: |
179822 次 |
| 最近记录: |