Java int to String - Integer.toString(i) vs new Integer(i).toString()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3930210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 07:05:23  来源:igfitidea点击:

Java int to String - Integer.toString(i) vs new Integer(i).toString()

javastringint

提问by marcolopes

Sometimes java puzzles me.
I have a huge amount of intinitializations to make.

有时java让我感到困惑。
我有大量的int初始化要做。

What's the realdifference?

什么是真正的差异?

  1. Integer.toString(i)
  2. new Integer(i).toString()
  1. Integer.toString(i)
  2. new Integer(i).toString()

采纳答案by Jean

Integer.toStringcalls the static method in the class Integer. It does not need an instance of Integer.

Integer.toString调用类中的静态方法Integer。它不需要 的实例Integer

If you call new Integer(i)you create an instance of type Integer, which is a full Java object encapsulating the value of your int. Then you call the toStringmethod on it to ask it to return a string representation of itself.

如果调用new Integer(i),则创建一个 type 实例Integer,它是一个完整的 Java 对象,封装了 int 的值。然后调用toString它的方法,要求它返回自身的字符串表示。

If all you want is to print an int, you'd use the first one because it's lighter, faster and doesn't use extra memory (aside from the returned string).

如果你只想打印一个int,你会使用第一个,因为它更轻、更快并且不使用额外的内存(除了返回的字符串)。

If you want an object representing an integer value—to put it inside a collection for example—you'd use the second one, since it gives you a full-fledged object to do all sort of things that you cannot do with a bare int.

如果你想要一个表示整数值的对象——例如将它放在一个集合中——你会使用第二个,因为它为你提供了一个完整的对象来完成你无法用裸int.

回答by oksayt

new Integer(i).toString()first creates a (redundant) wrapper object around i(which itself may be a wrapper object Integer).

new Integer(i).toString()首先创建一个(冗余)包装对象i(它本身可能是一个包装对象Integer)。

Integer.toString(i)is preferred because it doesn't create any unnecessary objects.

Integer.toString(i)是首选,因为它不会创建任何不必要的对象。

回答by Dhiraj

  1. new Integer(i).toString();

    This statement creates the object of the Integer and then call its methods toString(i)to return the String representation of Integer's value.

  2. Integer.toString(i);

    It returns the String object representing the specific int (integer), but here toString(int)is a staticmethod.

  1. new Integer(i).toString();

    此语句创建 Integer 的对象,然后调用其方法toString(i)返回 Integer 值的 String 表示

  2. Integer.toString(i);

    返回表示特定 int (integer) 的 String 对象,但这里toString(int)有一个static方法。

Summary is in first case it returns the objects string representation, where as in second case it returns the string representation of integer.

总结是在第一种情况下它返回对象字符串表示,而在第二种情况下它返回整数的字符串表示。

回答by Dhiraj

In terms of performance measurement, if you are considering the time performance then the Integer.toString(i);is expensive if you are calling less than 100 million times. Else if it is more than 100 million calls then the new Integer(10).toString()will perform better.

在性能测量方面,如果您正在考虑时间性能,那么Integer.toString(i); 如果您拨打的次数少于 1 亿次,则费用昂贵。否则,如果调用次数超过 1 亿次,那么new Integer(10).toString() 的性能会更好。

Below is the code through u can try to measure the performance,

下面是通过你可以尝试衡量性能的代码,

public static void main(String args[]) {
            int MAX_ITERATION = 10000000;
        long starttime = System.currentTimeMillis();
        for (int i = 0; i < MAX_ITERATION; ++i) {
            String s = Integer.toString(10);
        }
        long endtime = System.currentTimeMillis();
        System.out.println("diff1: " + (endtime-starttime));

        starttime = System.currentTimeMillis();
        for (int i = 0; i < MAX_ITERATION; ++i) {
            String s1 = new Integer(10).toString();
        }
        endtime = System.currentTimeMillis();
        System.out.println("diff2: " + (endtime-starttime));
    }

In terms of memory, the

在内存方面,

new Integer(i).toString();

新整数(i).toString();

will take more memory as it will create the object each time, so memory fragmentation will happen.

将占用更多内存,因为它每次都会创建对象,因此会发生内存碎片。

回答by Jasper Holton

I also highly recommend using

我也强烈推荐使用

int integer = 42;
String string = integer + "";

Simple and effective.

简单有效。

回答by ryu

Better:

更好的:

Integer.valueOf(i).toString()

回答by fhucho

Another option is the static String.valueOfmethod.

另一种选择是静态String.valueOf方法。

String.valueOf(i)

It feelsslightly more right than Integer.toString(i)to me. When the type of i changes, for example from intto double, the code will stay correct.

感觉略多于权Integer.toString(i)给我。当 i 的类型发生变化时,例如从intto double,代码将保持正确。

回答by Shailej Shimpi

Here Integer.toStringcalls the static method in the class Integer. It does not require the object to call.

这里 Integer.toString调用Integer类中的静态方法。它不需要对象来调用。

If you call new Integer(i)you first create an instance of type Integer, which is a full Java object encapsulating the value of your int i. Then you call the toStringmethod on it to ask it to return a string representation of itself.

如果你调用new Integer(i)你首先创建一个 Integer 类型的实例,它是一个完整的 Java 对象,封装了你的 int i 的值。然后调用toString它的方法,要求它返回自身的字符串表示。

回答by Nathan Waite

Although I like fhucho's recommendation of

虽然我喜欢 fhucho 的推荐

String.valueOf(i)

The irony is that this method actually calls

讽刺的是,这个方法实际上调用

Integer.toString(i)

Thus, use String.valueOf(i)if you like how it reads and you don't need radix, but also knowing that it is less efficient than Integer.toString(i).

因此,String.valueOf(i)如果您喜欢它的读取方式并且不需要基数,请使用它,但也要知道它的效率低于Integer.toString(i).

回答by Shiv Buyya

Simple way is just concatenate ""with integer:

简单的方法是""与整数连接:

int i = 100;

String s = "" + i;

now swill have 100as string value.

现在s将具有100字符串值。