There are multiple ways:
String.valueOf(number)(my preference)"" + number(I don't know how the compiler handles it, perhaps it is as efficient as the above)Integer.toString(number)
Java - Convert integer to string - Stack Overflow
c# - Convert int to string? - Stack Overflow
java - How do I convert from int to String? - Stack Overflow
How does computer converts integer to string?
Videos
There are multiple ways:
String.valueOf(number)(my preference)"" + number(I don't know how the compiler handles it, perhaps it is as efficient as the above)Integer.toString(number)
Integer class has static method toString() - you can use it:
int i = 1234;
String str = Integer.toString(i);
Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.
Normal ways would be Integer.toString(i) or String.valueOf(i).
The concatenation will work, but it is unconventional and could be a bad smell as it suggests the author doesn't know about the two methods above (what else might they not know?).
Java has special support for the + operator when used with strings (see the documentation) which translates the code you posted into:
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(i);
String strI = sb.toString();
at compile-time. It's slightly less efficient (sb.append() ends up calling Integer.getChars(), which is what Integer.toString() would've done anyway), but it works.
To answer Grodriguez's comment: ** No, the compiler doesn't optimise out the empty string in this case - look:
simon@lucifer:~$ cat TestClass.java
public class TestClass {
public static void main(String[] args) {
int i = 5;
String strI = "" + i;
}
}
simon@lucifer:~$ javac TestClass.java && javap -c TestClass
Compiled from "TestClass.java"
public class TestClass extends java.lang.Object{
public TestClass();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_5
1: istore_1
Initialise the StringBuilder:
2: new #2; //class java/lang/StringBuilder
5: dup
6: invokespecial #3; //Method java/lang/StringBuilder."<init>":()V
Append the empty string:
9: ldc #4; //String
11: invokevirtual #5; //Method java/lang/StringBuilder.append:
(Ljava/lang/String;)Ljava/lang/StringBuilder;
Append the integer:
14: iload_1
15: invokevirtual #6; //Method java/lang/StringBuilder.append:
(I)Ljava/lang/StringBuilder;
Extract the final string:
18: invokevirtual #7; //Method java/lang/StringBuilder.toString:
()Ljava/lang/String;
21: astore_2
22: return
}
There's a proposal and ongoing work to change this behaviour, targetted for JDK 9.
It's acceptable, but I've never written anything like that. I'd prefer this:
String strI = Integer.toString(i);
How does computer or programming languages out there converts binary integers into a string? what is the algorithm behind it? Like int 300 to string "300"
I am aware that the C standard provides plenty of functions for converting binary values into human-readable strings, but I am interested in learning how they actually accomplish this, particularly with integers.
Letโs say that as an educational exercise I am trying to convert integer values to strings without the help of LibC.
There are two (naive) approaches that come to my mind:
Manually create a giant lookup table containing the string representation for every possible value of the desired type.
Convert the numbers in the same way a human would, going bit by and adding together the corresponding powers of 2 as needed. If you create a function that adds numbers by manipulating their string representations directly, then you would only need a lookup table that contains the strings for the necessary powers of 2.
The second option is actually what I used a while back on a very basic big number library.
However, both of these options seem incredibly inefficient.
While every compiler/implementation will be a little different, is there some kind of common algorithm that is used to do this efficiently?
(Google has been unhelpful, either telling me to use the standard functions, or showing me the algorithm to turn binary strings into actual ints)
Edit: For simplicity, assume that a โstringโ is a typical ASCII, null-terminated char array/pointer.
Edit 2: Typos
Edit 3: Thank you for the helpful comments :)