相关疑难解决方法(0)

如何在Java中将String转换为int?

我怎么能转换StringintJava中?

我的字符串只包含数字,我想返回它代表的数字.

例如,给定字符串"1234",结果应该是数字1234.

java string int type-conversion

2882
推荐指数
27
解决办法
580万
查看次数

Why do == comparisons with Integer.valueOf(String) give different results for 127 and 128?

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 …

java comparison integer

179
推荐指数
5
解决办法
2万
查看次数

Integer.valueOf()与Integer.parseInt()

除了Integer.parseInt()处理减号(如文件所述),Integer.valueOf()和之间是否还有其他差异Integer.parseInt()

因为两者都不能解析,十进制千位分隔符(产生NumberFormatException),有没有一个已经可用的Java方法来做到这一点?

java string integer decimal-point

174
推荐指数
5
解决办法
19万
查看次数

在java中将String转换为Integer的最有效方法

将String转换为Integer对象的方法有很多种.以下哪项是最有效的:

Integer.valueOf()
Integer.parseInt()
org.apache.commons.beanutils.converters.IntegerConverter
Run Code Online (Sandbox Code Playgroud)

我的用例需要创建包装器Integer对象...意味着没有原始int ...并且转换后的数据用于只读.

java optimization performance

25
推荐指数
8
解决办法
5万
查看次数

为什么JVM允许为IntegerCache设置"高"值,而不是"低"?

我们都知道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)

java jvm-arguments

20
推荐指数
1
解决办法
750
查看次数

哪一个更快?Integer.valueOf(String string)还是Integer.parseInt(String string)?

我想知道哪种方法更快?

Integer.valueOf(String string)还是Integer.parseInt(String string)

这两种方法之间是否存在性能或内存差异?

在java中看到了parseInt和valueOf之间的区别?但这并没有解释性能方面的差异.

java android

8
推荐指数
2
解决办法
1万
查看次数

在检查平等时将字符串转换为Int或Int转换为字符串是否更高效?

以下设置:

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)

java android

8
推荐指数
1
解决办法
665
查看次数

Long.getLong(s)、Long.valueOf(s)、Long.parseLong(s) 之间的区别,其中“s”是字符串类型

我想将字符串转换为长整型。但我发现了 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 difference

2
推荐指数
1
解决办法
6969
查看次数

int parseInt(String s) 和 Integer.valueOf(String s) 有什么区别?还有什么是带有数值参数 {10 或 etc} 的基数

我在 Java 和 BlueJ 中使用过这两个,但我不确定这两者之间的区别在哪里。在我的书中对 parseInt 的描述中也提到了基数 10。基数 10 到底是什么?

java

0
推荐指数
1
解决办法
146
查看次数