1. Overview
1.概述
As we know, conversion from int to String is a very common operation in Java.
正如我们所知,从int 到String 的转换是Java中非常常见的操作。
In this short tutorial, we’ll go through two very popular methods, toString() of the Integer class and valueOf() of the String class, which helps us with this conversion. Moreover, we’ll also look at a few points and examples using these two methods to understand it better.
在这个简短的教程中,我们将通过两个非常流行的方法,类的Integer 的toString()和类的valueOf(),帮助我们进行这种转换。此外,我们还将看一下使用这两种方法的几个要点和例子,以便更好地理解它。
2. The Integer.toString() method
2.Integer.toString()方法
This method accepts an integer of primitive data type int as a parameter and returns a String object representing the specified integer.
该方法接受一个原始数据类型为int的整数作为参数,并返回一个代表指定整数的String对象。
Let’s look at its signature:
让我们来看看它的签名。
public static String toString(int i)
Now, we’ll see a few examples where we pass signed/unsigned integers as parameters to it to understand integer to string conversion happens:
现在,我们将看到几个例子,我们将有符号/无符号整数作为参数传递给它,以了解整数到字符串的转换发生。
@Test
public void whenValidIntIsPassed_thenShouldConvertToString() {
assertEquals("11", Integer.toString(11));
assertEquals("11", Integer.toString(+11));
assertEquals("-11", Integer.toString(-11));
}
3. The String.valueOf() method
3.String.valueOf() 方法
This method also accepts an integer of primitive data type int as a parameter and returns a String object. Interestingly, the returned String representation is exactly the same as the one returned by the Integer.toString(int i) method. It is because internally, it uses Integer.toString() method.
这个方法也接受一个原始数据类型int的整数作为参数并返回一个String对象。有趣的是,返回的字符串表示法与Integer.toString(int i)方法返回的完全一样。这是因为在内部,它使用了Integer.toString()方法。
Let’s look at its internal implementation as given in java.lang.String class:
让我们看看它在java.lang.String类中的内部实现。
/**
* Returns the string representation of the {@code int} argument.
* <p>
* The representation is exactly the one returned by the
* {@code Integer.toString} method of one argument.
*
* @param i an {@code int}.
* @return a string representation of the {@code int} argument.
* @see java.lang.Integer#toString(int, int)
*/
public static String valueOf(int i) {
return Integer.toString(i);
}
To understand it better, we’ll see a few examples where we pass signed/unsigned integers as parameters to it to understand integer to string conversion happens:
为了更好地理解它,我们将看到几个例子,我们将有符号/无符号整数作为参数传递给它,以了解整数到字符串的转换情况。
@Test
public void whenValidIntIsPassed_thenShouldConvertToValidString() {
assertEquals("11", String.valueOf(11));
assertEquals("11", String.valueOf(+11));
assertEquals("-11", String.valueOf(-11));
}
4. Differences Between Integer.toString() and String.valueOf()
4.Integer.toString()和String.valueOf()之间的区别
To summarize, there are no actual differences between these two methods, but we should understand the following points to avoid confusion.
总而言之,这两种方法之间没有实际差异,但我们应该了解以下几点,以避免混淆。
There is one extra call in the stack trace when we use String.valueOf() method because it internally uses the same Integer.toString() method.
当我们使用String.valueOf()方法时,堆栈跟踪中有一个额外的调用,因为它内部使用相同的Integer.toString()方法。
There can be some confusion when passing a null object to valueOf() method because, when passing a primitive int to valueOf() method as it looks the same, but the actual method call goes to a different overloaded method.
当把一个null对象传递给valueOf()方法时,可能会有一些混淆,因为 当把一个原始的int传递给valueOf()方法时,因为它看起来是一样的,但是实际的方法调用是到一个不同的重载方法。
Integer.toString() might throw NullPointerException if given Integer object is null. String.valueOf() won’t throw an exception because it’ll go to String.valueOf(Object obj) method and return null. Please note, primitive int passed to String.valueOf(int i) can never be null but since there is another method String.valueOf(Object obj), we can get confused between the two overloaded methods.
Integer.toString()可能会抛出NullPointerException,如果给定的Integer对象是null。String.valueOf()不会抛出异常,因为它会进入String.valueOf(Object obj) 方法并返回null。请注意,传递给String.valueOf(int i)的primitive int永远不可能是null,但由于有另一个方法String.valueOf(Object obj),我们会在这两个重载方法之间混淆。
Let’s understand the last point with the following example:
让我们通过下面的例子来理解最后一点。
@Test(expected = NullPointerException.class)
public void whenNullIntegerObjectIsPassed_thenShouldThrowException() {
Integer i = null;
System.out.println(String.valueOf(i));
System.out.println(i.toString());
}
Please note that primitive int can never be null, we’re checking it in case the exception is thrown by methods below it.
请注意,原始的int不可能是null,我们检查它是为了防止它下面的方法抛出异常。
5. Effect of JVM Method Inlining on String.valueOf() method
5.JVM方法嵌入对String.valueOf()方法的影响
As we discussed earlier, String.valueOf() method involves an extra call. But, JVM can eliminate even that additional call in the stack trace by method inlining.
正如我们前面所讨论的,String.valueOf()方法涉及一个额外的调用。但是,JVM甚至可以通过方法内联来消除堆栈跟踪中的额外调用。
However, this depends entirely on whether JVM chooses to inline that method. For a more detailed description, please visit our article on Method Inlining in the JVM.
然而,这完全取决于JVM是否选择内联该方法。关于更详细的描述,请访问我们关于JVM中方法内联的文章。
6. Conclusion
6.结语
In this article, we learnt about the Integer.toString() and String.valueOf() methods. We also looked at a few points where we should concentrate to avoid confusion while programming.
在这篇文章中,我们学习了Integer.toString()和String.valueOf()方法。我们还研究了一些我们应该关注的问题,以避免在编程时出现混乱。
As always, the complete code samples for this article can be found over on GitHub.
一如既往,本文的完整代码样本可以在GitHub上找到。