我怎么能转换String成intJava中?
我的字符串只包含数字,我想返回它代表的数字.
例如,给定字符串"1234",结果应该是数字1234.
I have no idea why these lines of code return different values:
System.out.println(Integer.valueOf("127")==Integer.valueOf("127"));
System.out.println(Integer.valueOf("128")==Integer.valueOf("128"));
System.out.println(Integer.parseInt("128")==Integer.valueOf("128"));
Run Code Online (Sandbox Code Playgroud)
The output is:
true
false
true
Run Code Online (Sandbox Code Playgroud)
Why does the first one return true and the second one return false? Is there something different that I don't know between 127 and 128? (Of course I know that 127 < 128.)
Also, why does the third one return true?
I have read the answer of this question, but I still didn't get …
除了Integer.parseInt()处理减号(如文件所述),Integer.valueOf()和之间是否还有其他差异Integer.parseInt()?
因为两者都不能解析,为十进制千位分隔符(产生NumberFormatException),有没有一个已经可用的Java方法来做到这一点?
将String转换为Integer对象的方法有很多种.以下哪项是最有效的:
Integer.valueOf()
Integer.parseInt()
org.apache.commons.beanutils.converters.IntegerConverter
Run Code Online (Sandbox Code Playgroud)
我的用例需要创建包装器Integer对象...意味着没有原始int ...并且转换后的数据用于只读.
我们都知道Java有一个缓存Integer(和其他一些类型)的数量在该范围内[-128, 127]被认为是"常用".
缓存的设计如下:
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the …Run Code Online (Sandbox Code Playgroud) 我想知道哪种方法更快?
Integer.valueOf(String string)还是Integer.parseInt(String string)?
这两种方法之间是否存在性能或内存差异?
我在java中看到了parseInt和valueOf之间的区别?但这并没有解释性能方面的差异.
以下设置:
int a=3;
String b="3";
Run Code Online (Sandbox Code Playgroud)
两个变量都表示在语义上相等的ID.由于应用程序适用于移动设备,因此以尽可能最有效的方式完成这些变量的比较非常重要.
将这些变量与此代码段进行比较是否有效,
boolean areEqual = Integer.parseInt(b) == a;
Run Code Online (Sandbox Code Playgroud)
或者这个?
boolean areEqual = String.valueOf(a).equals(b);
Run Code Online (Sandbox Code Playgroud) 我想将字符串转换为长整型。但我发现了 4 种不同的方法来归档该提案。
Long.getLong(s) - 确定具有指定名称的系统属性的长整型值。
Long.valueOf(s) - 返回保存指定 String 值的 Long 对象
Long.parseLong(s) - 将字符串参数解析为带符号的十进制长整型。
new Long(s) - 构造一个新分配的 Long 对象,表示 String 参数指示的 long 值
除此之外,“parseLong()”返回一个 long 值,其他 3 个返回 Long 对象。它们之间有什么区别,它们的最佳使用情况是什么?(何时使用它们),哪一种性能更好?
提前致谢。
这给了我 "valueOf(s)" 和 "new Long(s)" 之间的区别,并且还发现了"valueOf(s)" 和 "Long.parseLong(s)" 之间的区别。
但我仍然不明白 Long.getLong(s) 的用途。“确定具有指定名称的系统属性的长值”是什么意思?
我在 Java 和 BlueJ 中使用过这两个,但我不确定这两者之间的区别在哪里。在我的书中对 parseInt 的描述中也提到了基数 10。基数 10 到底是什么?
java ×9
android ×2
integer ×2
string ×2
comparison ×1
difference ×1
int ×1
optimization ×1
performance ×1