简体   繁体   English

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

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

Sometimes java puzzles me. 有时java困惑我。
I have a huge amount of int initializations to make. 我有大量的int初始化。

What's the real difference? 真正的区别是什么?

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

Integer.toString calls the static method in the class Integer . Integer.toString调用Integer类中的静态方法。 It does not need an instance of 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. 如果调用new Integer(i) ,则创建一个Integer类型的实例,它是一个封装int值的完整Java对象。 Then you call the toString method on it to ask it to return a string representation of itself . 然后在其上调用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做的所有事情。 。

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)是首选,因为它不会创建任何不必要的对象。

Another option is the static String.valueOf method. 另一个选项是静态String.valueOf方法。

String.valueOf(i)

It feels slightly more right than Integer.toString(i) to me. 感觉Integer.toString(i)对我更正确。 When the type of i changes, for example from int to double , the code will stay correct. 当i的类型改变时,例如从int变为double ,代码将保持正确。

I also highly recommend using 我也强烈推荐使用

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

Simple and effective. 简单有效。

  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 . 此语句创建Integer的对象,然后调用其方法toString(i)返回Integer值的String表示形式

  2. Integer.toString(i);

    It returns the String object representing the specific int (integer) , but here toString(int) is a static method. 返回表示特定int(整数)的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. 摘要在第一种情况下它返回对象字符串表示,其中在第二种情况下它返回整数的字符串表示。

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)

In terms of performance measurement, if you are considering the time performance then the Integer.toString(i); 在性能测量方面,如果您正在考虑时间性能,那么Integer.toString(i); is expensive if you are calling less than 100 million times. 如果你打电话的次数少于1亿次,那就太贵了。 Else if it is more than 100 million calls then the new Integer(10).toString() will perform better. 否则,如果它超过1亿次调用​​,则新的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(); new Integer(i).toString();

will take more memory as it will create the object each time, so memory fragmentation will happen. 将占用更多内存,因为它每次都会创建对象,因此会发生内存碎片。

更好:

Integer.valueOf(i).toString()

Simple way is just concatenate "" with integer: 简单的方法就是用整数连接""

int i = 100;

String s = "" + i;

now s will have 100 as string value. 现在s将有100作为字符串值。

Here Integer.toString calls the static method in the class Integer. 这里Integer.toString调用Integer类中的静态方法。 It does not require the object to call. 它不需要对象调用。

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. 如果调用new Integer(i) ,首先要创建一个Integer类型的实例,它是一个封装int i值的完整Java对象。 Then you call the toString method on it to ask it to return a string representation of itself. 然后在其上调用toString方法,要求它返回自身的字符串表示。

1. Integer.toString(i) 1. Integer.toString(i)

Integer i = new Integer(8);
    // returns a string representation of the specified integer with radix 8
 String retval = i.toString(516, 8);
System.out.println("Value = " + retval);

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

 int i = 506;

String str = new Integer(i).toString();
System.out.println(str + " : " + new Integer(i).toString().getClass());////506 : class java.lang.String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM