![]() |
VOOZH | about |
Converting an int to a String is an important type conversion. Many operations can be performed over a string, while we are limited when it comes to integers. We have a wide varied list of in-built methods in the String class that help us perform hassle-free operations.
Suppose we are required to concatenate two integers then it would become a tedious for us. We need to go through as we need to deal with the number system, corresponding to which we will be playing mathematics within the number system. But to convert integers to strings in Java, we have some inbuilt methods and classes which make our work too easy.
Tip: We generally convert primitive class data members types though we have the concept of Wrapper classes to Strings because in practical programming in java we deal with strings.
Converting Integers to Strings involves using the Integer classes toString() or String.valueOf() for direct conversion. String.format() is another method offering flexible formatting options. Using StringBuilder or StringBuffer for appending integer values as strings is efficient for extensive string manipulation.
There are certain methods for Integer to String conversions mentioned below:
The Integer class has a static method toString() that returns a String object representing the specified int parameter. The argument is converted and returned as a string instance. If the number is negative, the sign will be preserved.
Example:
String str1 = 1234 String str2 = -1234
The String class has a static method valueOf() that can be used to convert the Integer to String as shown below:
Example:
String str3 = 1234
It is different from method 1 as proposed above, as in this method we use an instance of the Integer class to invoke its toString() method.
Example:
String str4 = 1234
Explanation: If the variable is of primitive type (int), it is better to use Integer.toString(int) or String.valueOf(int). But if the variable is already an instance of Integer (wrapper class of the primitive type int), it is better to just invoke its toString() method as shown above.
Note: This method is not efficient as an instance of the Integer class is created before conversion is performed. And deprecated and marked as removal.
Here, we will declare an empty string and using the '+' operator, we will simply store the resultant as a string. Now by this, we are successfully able to append and concatenate these strings.
Example:
String str1 = 1234 String str2 = -1234
There are certain advance Methods are mentioned below:
DecimalFormat is a class that formats a number to a String.
Example:
12,345
Tip: Using this method, We can specify the number of decimal places and comma separator for readability.
StringBuffer is a class that is used to concatenate multiple values into a String.
Example 1:
String str6 = 1234
Example 2:
String str6 = 1234
StringBuilder works similarly but is not thread-safe like StringBuffer.
Example 1:
String str7 = 1234
Example 2:
String str7 = 1234
Note: All the examples above use the base (radix) 10. Following are convenient methods to convert to binary, octal, and hexadecimal systems. The arbitrary custom number system is also supported.
We can also convert int to String in different bases below are the some of the important methods are mentioned which help to convert int to String with different base.
Example 1: Converting int(base 2 or binary number) to String using Integer class method toBinary().
11111111
Explanation: 11111111 is the binary representation of the number 255.
Example 2: Converting int(base 8 or octal number) to String using Integer class method toOctalString().
377
Explanation: 377 is the octal representation of the number 255.
Example 3: Converting int(base 10 or Hexadecimal number) to String using Integer class method toHexString().
ff
Explanation: The ff is the hexadecimal representation of the number 255.
Approach: We are using the toString() method of the Integer class to get it converted into a string where additionally we will be passing a value as an argument known as radix. One can use any other custom base/radix when converting an int to a string. In the example below, we are considering the base 7 number system for illustration purposes.
Example: Converting int(base 7 or custom radix number) to String using Integer class method toString(int I, int radix).
513
Note: 513 is the representation of the number 255 when written in the base 7 system.