If you ask any java developer how to convert int to string , then most of them will answer by using valueOf() method . Java interview questions difficulty level starts to increase when interviewer adds some conditions to the java interview questions. So if java interviewer asks please write the algorithm to convert signed integer to string. Here most of the students puzzled .Next question after this will be
Read Also : How to convert String to int without using ParseInt() method code with example
Interviewer : What are the different ways to convert integer to string ? Which is the most efficient and Why ?
Thus to make sure ,you will not get puzzled when bombarded with such type of java interview questions , lets discuss the answer in detail .
Method 1 : Creating our own algorithm
Logic :
To convert values to characters, we need to know what those values are. So lets find out how to achieve this. Suppose you have the number 537. Looking at this number’s decimal representation , it seems a simple matter to identify the digit values 5, 3, and 7. But we know that the computer isn’t using a decimal representation, but rather the binary representation 1000011001. As we can’t select decimal digits directly from a binary number, we must calculate the value of each digit. It seems logical to try to find the digit values either left to right or right to left.Here ,right to left is much efficient than left to right.So we are taking
approach to find the digit values from right to left.
To implement this algorithm, you must find the place value of the first digit and divide the place value by 10 for each new digit. Modulo (%)gives the remainder of an integer division. We just divide and modulo until there’s nothing left.
There is one pitfall, we don’t know how many digits there will be until we’ve found them all.The solution to this problem is : To write the digits out backward as you discover them and then reverse them into the proper order when you’re done.
Example of Converting Signed Integer to String Java
Here to convert signed int to string in java, two case arises :
1. If number is positive (+ve) :
Input : 537 Output : "537"
2. If number is negative (-ve)
If the number is negative , we will make it positive at the start by negating it .So treating it positive , we will do the operations as explained above. At end , we will add the "-" sign to the result .
Input : -537 Output : "-537"
Pseudo Algorithm :
1. If number less than zero:
Negate the number
Set negative flag
2. While number not equal to 0
Add '0' to number % 10 and write this to temp buffer
Integer divide number by 10
3. If negative flag is set
4. Write '-' into next position in temp buffer
5. Write characters in temp buffer into output string in reverse order:
Code for Converting Integer to String in Java :
Read Also : How to convert String to int without using ParseInt() method code with example
Interviewer : What are the different ways to convert integer to string ? Which is the most efficient and Why ?
Thus to make sure ,you will not get puzzled when bombarded with such type of java interview questions , lets discuss the answer in detail .
Method 1 : Creating our own algorithm
Logic :
To convert values to characters, we need to know what those values are. So lets find out how to achieve this. Suppose you have the number 537. Looking at this number’s decimal representation , it seems a simple matter to identify the digit values 5, 3, and 7. But we know that the computer isn’t using a decimal representation, but rather the binary representation 1000011001. As we can’t select decimal digits directly from a binary number, we must calculate the value of each digit. It seems logical to try to find the digit values either left to right or right to left.Here ,right to left is much efficient than left to right.So we are taking
approach to find the digit values from right to left.
To implement this algorithm, you must find the place value of the first digit and divide the place value by 10 for each new digit. Modulo (%)gives the remainder of an integer division. We just divide and modulo until there’s nothing left.
There is one pitfall, we don’t know how many digits there will be until we’ve found them all.The solution to this problem is : To write the digits out backward as you discover them and then reverse them into the proper order when you’re done.
Example of Converting Signed Integer to String Java
Here to convert signed int to string in java, two case arises :
1. If number is positive (+ve) :
Input : 537 Output : "537"
2. If number is negative (-ve)
If the number is negative , we will make it positive at the start by negating it .So treating it positive , we will do the operations as explained above. At end , we will add the "-" sign to the result .
Input : -537 Output : "-537"
Pseudo Algorithm :
1. If number less than zero:
Negate the number
Set negative flag
2. While number not equal to 0
Add '0' to number % 10 and write this to temp buffer
Integer divide number by 10
3. If negative flag is set
4. Write '-' into next position in temp buffer
5. Write characters in temp buffer into output string in reverse order:
Code for Converting Integer to String in Java :
public class intToString { public static void main(String args []) { String result= intToStr(23); System.out.println("Output is : "+ result); } public static final int MAX_DIGITS = 10;
public static String intToStr( int num ){ int i = 0; boolean isNegative = false; /* Buffer big enough for largest int and - sign */ char[] temp = new char[ MAX_DIGITS + 1 ]; /* Check to see if the number is negative */ if( num < 0 ){ num = -num; isNegative = true; } /* Fill buffer with digit characters in reverse order */ do { temp[i++] = (char)((num % 10) + '0'); num /= 10; } while( num != 0 ); StringBuilder b = new StringBuilder(); if( isNegative ) b.append( '-' ); while( i > 0 ){ b.append( temp[--i] ); } return b.toString(); } }
Method 2 : Convert int to string using valueOf() method
int number=537; String result = String.valueOf(number); System.out.println(result);
Method 3 : Convert int to string using toString() method
int number=537; String result = Integer.toString(number); System.out.println(result);
Method 4 : Append empty string "" to the integer using + operator
int number=537; System.out.println(number + "" );
Big Question !! Which is most efficient method to convert integer to string?and Why ?
We are discussing comparison between Method 2 , Method 3 and Method 4 , since the Method 1 is our own algorithm , we are not comparing it with the rest.
So the Order is :
Method 3 (recommended)> Method 2 > Method 4 (least preferred)
Why ?
Method 4 is the least preferred because
It uses empty string "" with + operator which internally performs :
StringBuilder sbobject= new StringBuilder(); sbobject.append(); sbobject.append(number); return sbobject.toString();
So there is so much overhead before it reaches toString() method.
Method 2 ,valueOf() method internal implementation is like this :
public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }
Above, we can see that valueOf() internally calling toString() method , to convert integer to string.
Hence Method 3 is the most efficient where we are directly calling toString() method to achieve our goal .
Please mention in the comments if you know any other way of converting integer to string.