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)
Answer from Bozho on Stack Overflow
๐ŸŒ
UltaHost
ultahost.com โ€บ knowledge-base โ€บ convert-java-string-to-integer
How to Convert String to Integer in Java | Ultahost Knowledge Base
May 17, 2025 - These inputs are presented as text or a string type, but you still need to utilize those numbers in calculations or logic. If you do not convert those strings to integers then the program may throw an exception or return the incorrect output. In this article, you will learn simple ways and reliable ways to convert a string to int Java.
Discussions

Converting a Number to a String
The docs will answer that question quite clearly https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString(int) https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#valueOf(int) under the String.valueOf(int) description: The representation is exactly the one returned by the Integer.toString method of one argument. to make it even more clear if we look at the source code for String.valueOf(int) https://hg.openjdk.java.net/jdk7u/jdk7u6/jdk/file/8c2c5d63a17e/src/share/classes/java/lang/String.java we find that it literally just calls Integer.toString(int) public static String valueOf(int i) { return Integer.toString(i); } More on reddit.com
๐ŸŒ r/javahelp
3
6
August 14, 2022
Java convert an Int to a String | Go4Expert
Discussion in 'Java' started by tombezlar, Mar 24, 2021. More on go4expert.com
๐ŸŒ go4expert.com
March 24, 2021
String to integer conversion in Java - General Web Dev - SitePoint Forums | Web Development & Design Community
Itโ€™s giving me a NumberFormatException error. How to solve this?? I have also read a couple of resources on string to integer in java on wiki and Scaler Topics to refresh my understanding of the string to integer in java. More on sitepoint.com
๐ŸŒ sitepoint.com
0
July 8, 2022
Why do you need to convert a String into an int if you enter an int? int answer=Integer.parsel(answerToLife);
Joseph Davis is having issues with: Why do you need to convert a String into an int if you enter an int? int answer=Integer.parsel(answerToLife); Also, does java recogonis... More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
3
February 9, 2017
People also ask

Is Integer.valueOf() slower than Integer.parseInt()?

Yes, Integer.valueOf() can be slightly slower because it returns an Integer object, which requires additional object creation. Use parseInt() for better performance with primitive int values.

๐ŸŒ
ultahost.com
ultahost.com โ€บ knowledge-base โ€บ convert-java-string-to-integer
How to Convert String to Integer in Java | Ultahost Knowledge Base
What is the difference between Integer.parseInt() and Integer.valueOf()?

 Integer.parseInt() returns a primitive int, while Integer.valueOf() returns an Integer object. Use parseInt() for primitive values and valueOf() when working with objects or collections.

๐ŸŒ
ultahost.com
ultahost.com โ€บ knowledge-base โ€บ convert-java-string-to-integer
How to Convert String to Integer in Java | Ultahost Knowledge Base
Can Integer.parseInt() handle non-numeric characters?

No, Integer.parseInt() throws a NumberFormatException if the string contains non-numeric characters. Ensure the string contains only digits or handle exceptions.

๐ŸŒ
ultahost.com
ultahost.com โ€บ knowledge-base โ€บ convert-java-string-to-integer
How to Convert String to Integer in Java | Ultahost Knowledge Base
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ int-to-string-java
Int to String Java Conversion: Methods and Examples
February 26, 2024 - In this guide, weโ€™ll walk you through the process of converting integers to strings in Java, from the basics to more advanced techniques.
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ converting a number to a string
r/javahelp on Reddit: Converting a Number to a String
August 14, 2022 -

Hey, all. I was working on some Codewars fundamentals and I came across this problem. My solution was accepted and, on the list of acceptable solutions, I see what seems to be an equally useful solution. I just wondered if there's any reason to use one over the other in any particular situation or if they do the same thing all the time. The passed in variable was some integer num.

My solution was:

return Integer.toString(num);

The other solution I saw was:

return String.valueOf(num);

Any insight would be much appreciated. Thanks!

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ different-ways-for-integer-to-string-conversions-in-java
Java Convert int to String | How to Convert an Integer into a String - GeeksforGeeks
April 9, 2025 - 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. ... // Java Program to Illustrate Integer to String Conversions // Using Concatenation with Empty String class Geeks { // Main driver method public static void main(String args[]) { // Custom integer values int a = 1234; int b = -1234; // Concatenating with empty strings String str1 = "" + a; String str2 = "" + b; // Printing the concatenated strings System.out.println("String str1 = " + str1); System.out.println("String str2 = " + str2); } }
๐ŸŒ
HappyCoders.eu
happycoders.eu โ€บ java โ€บ how-to-convert-int-to-string-fastest
How to Convert int to String in Java โ€“ The Fastest Way
December 2, 2024 - Extensive benchmark tests have shown that across all Java versions, "" + i is the fastest way to convert an integer to a String.
Find elsewhere
๐ŸŒ
Go4Expert
go4expert.com โ€บ forums โ€บ java-convert-int-string-t35421
Java convert an Int to a String | Go4Expert
March 24, 2021 - Here are 3 ways to convert an int to a String String s = String.valueOf(n); String s = Integer.toString(n); String s = "" + n; I understand what we...
๐ŸŒ
Initial Commit
initialcommit.com โ€บ blog โ€บ int-to-string-java
Convert Int to String Java
April 22, 2021 - A quick and dirty way to convert a number to string in Java is to simply concatenate the number to an empty string using the + operator. In addition to converting an int to string, this approach can also be used as a quick way to build longer ...
๐ŸŒ
SitePoint
sitepoint.com โ€บ general web dev
String to integer conversion in Java - General Web Dev - SitePoint Forums | Web Development & Design Community
July 8, 2022 - The colon is what is giving you the problem. Parse to int is intended to just that - parse a value to an integer (whole numbers). The parser doesnโ€™t know how to handle it. What would 16:45:20 convert to? 164520? If so, do a string replace to strip out the colons, THEN do the parse.
Top answer
1 of 5
7

Things I like about your code

  • The idea to calculate the length of a number with the logarithm is really good!
  • In my opinion you are writing good comments.
  • Good variable names
  • Works as intended, without (to my knowledge) any bugs.

Criticism

returnDigitString()

  • It is considered bad practice to put more than one command into one line. So please make line breaks after every ";".
  • Your solution is pretty long (over 30 lines) in comparison to the complexity of the problem. You could also have done something like that:
    public static String returnDigitString(int digit) {
        String res = "";
        String[] digits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        for(int i = 0; i <= 9; i++) {
            if(digit == i) {
                res += digits[i];
                break;
            }
        }
        return res;
    }

main()

  • You are not using the array "reverseStr". The String "digits" is not used either.
  • When I started your program the first time, I didn't know what to do, because your program didn't tell me. Before scanning a user input, I would tell the user to input something.
System.out.println("Please enter number:");
Scanner scn = new Scanner(System.in);
int number = scn.nextInt();

If you want to improve this point even further (which is highly recommended!), you can use something like that (you will have to use import java.util.InputMismatchException;):

System.out.println("Please enter number:");
Scanner scn = new Scanner(System.in);
int number;
while(true) {
    try {
        number = scn.nextInt();
        break;
    }
    catch(InputMismatchException e) {
        System.out.println("That's not a number!");
        scn.nextLine();
    }
}

This will check, whether the user really enters a number. If the user enters something else, the program will ask him again to enter a number.

  • Something like that is considered bad practice:
if(number != 0) {
length = ( int ) (Math.log10(number) + 1 );}

Please write

if(number != 0) {
        length = (int) (Math.log10(number) + 1);
}

instead.

  • "valStr" is not necessary. You can just write:
strSeq = returnDigitString(remainder) + strSeq;

But this really is a minor point and just my personal opinion. It's fine to use an extra variable for this.

Codestructure

  • I would use an extra method for the content of the main-method. Just use the main-method to call the new method.
2 of 5
9

Personally I think your algorithm has been made a lot more complex than needed.

Is the concatenation a requirement? If not, you can simplify by directly converting each digit into a char and storing it in a char[]. This way instead of inefficiently concatenating each digit onto the string, you can use the string constructor overload that takes a char[].

With this simplification, the method can be reduced to just a few lines:

  public static String intToString(int num) {
    if(num == 0){
      return "0";
    }
    int count = 0;
    boolean isNeg = false;
    if (num < 0) {
      num *= -1;
      count = 1;
      isNeg = true;
    }
    count += (int) Math.log10(num) + 1;
    char[] digits = new char[count];
    if (isNeg) {
      digits[0] = '-';
    }
    --count;
    while(num > 0) {
      digits[count--] = (char) ((num % 10) + '0');
      num /= 10;
    }
    return new String(digits);
  }
๐ŸŒ
Pega
support.pega.com โ€บ question โ€บ convert-int-string
Convert Int to String | Support Center
January 12, 2017 - I couldn't find any OOTB function to acheive this requirement, however you may use the below methods in a Java step to acheive your requirement. - Convert using Integer.toString(int) - Convert using String.valueOf(int) - Convert using new Integer(int).toString() To see attachments, please log in.
๐ŸŒ
WeTalkIt
wetalkitorg.wordpress.com โ€บ 2020 โ€บ 02 โ€บ 10 โ€บ how-to-convert-an-integer-to-string-in-java
How to convert an integer to String in Java โ€“ WeTalkIt
February 25, 2020 - Intro There are many common situations ... this tutorial is to present different ways to achieve this goal. Using toString Using the toString method ......
๐ŸŒ
Scaler
scaler.com โ€บ topics โ€บ string-to-int-in-java
Java Convert String to int - Scaler Topics
February 21, 2024 - Integer.valueOf() method returns the Integer object when a string literal is passed as an argument. It takes in the string or an integer as a first argument and converts it into an Integer object.
๐ŸŒ
Google Groups
groups.google.com โ€บ g โ€บ google-web-toolkit โ€บ c โ€บ BjUE-T5d6b8
String to int conversion
July 8, 2007 - > > > please tell!!! > > > On Jul 8, 8:45 am, konquerror <konquer...@gmail.com> wrote: > > > Hii all > > > > i am trying to convert 1 string to integer. > > > > using the command > > > > int img=(int)Integer.valueOf(s4); > > > > It works all fine in Java, as i tested it separately too.....
๐ŸŒ
Udemy
blog.udemy.com โ€บ home โ€บ how to convert integers to strings in java
How to Convert Integers to Strings in Java - Udemy Blog
December 4, 2019 - The above code will store the numeric value as a String without the error. There are three ways to convert an integer to a string:
๐ŸŒ
Udemy
blog.udemy.com โ€บ home โ€บ how to convert a java string to integer
How to Convert a Java String to Integer - Udemy Blog
December 4, 2019 - Java provides many functions for string manipulations. A few frequently used ones are: ... String input is often taken from the user to perform manipulations. The advantage the string class provides is that you can easily convert strings to other forms of data types like an integer or float.