|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "How To Convert An Integer To String In Java" |
| 4 | +author: gaurav |
| 5 | +image: assets/images/2021-02-27/convert-int-to-string.webp |
| 6 | +categories: [Java, Core Java, String] |
| 7 | +description: "In this article you are going to learn how can you convert an integer to a String." |
| 8 | +--- |
| 9 | + |
| 10 | +In this article you are going to learn how can you convert an integer to a String. |
| 11 | + |
| 12 | +There are multiple ways we can convert an int to string in java. let's check what are they. |
| 13 | + |
| 14 | +1. `String.ValueOf()` method |
| 15 | +2. `Integer.toString()` method |
| 16 | +3. String.format() method |
| 17 | + |
| 18 | +## 1. `String.valueOf()` method |
| 19 | + |
| 20 | +`String.valueOf()` method returns the string representation of the `Object` argument. |
| 21 | + |
| 22 | +So when we pass an int as parameter to `valueOf()` method, it returns it's String representation. |
| 23 | + |
| 24 | +If the argument is `null`, then a string equal to `"null"`will be returned. |
| 25 | + |
| 26 | +Internally `valueOf()` method calls `toString()` method on the object argument to get its string representation. |
| 27 | + |
| 28 | +An example to convert an int to string using `valueOf()` method is given below. |
| 29 | + |
| 30 | +```java |
| 31 | +/* |
| 32 | + * A program to convert an int to string in java using |
| 33 | + * the String.toSTring() method. |
| 34 | + * |
| 35 | + * @author Gaurav Kukade at coderolls.com |
| 36 | + */ |
| 37 | + |
| 38 | +public class IntToSTring{ |
| 39 | + |
| 40 | + public static void main(String []args){ |
| 41 | + |
| 42 | + int number = 1234; |
| 43 | + |
| 44 | + String numberString = String.valueOf(number); |
| 45 | + |
| 46 | + //print string value of number |
| 47 | + System.out.println("numberString: " + numberString); |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | +Output |
| 52 | +```java |
| 53 | +numberString: 1234 |
| 54 | +``` |
| 55 | + |
| 56 | +## 2. `Integer.toString()` method |
| 57 | + |
| 58 | +`Integer.toString()` method returns the String object of the Integer's value. |
| 59 | + |
| 60 | +The value is converted to signed decimal representation and returned as a string, exactly as if the integer value were given as an argument to the `toString()` method. |
| 61 | + |
| 62 | +An example to convert an int to string using the `toString()` method is given below. |
| 63 | + |
| 64 | +```java |
| 65 | +/* |
| 66 | + * A program to convert an int to string in java using |
| 67 | + * the Integer.toString() method. |
| 68 | + * |
| 69 | + * @author Gaurav Kukade at coderolls.com |
| 70 | + */ |
| 71 | + |
| 72 | +public class IntToSTring{ |
| 73 | + |
| 74 | + public static void main(String []args){ |
| 75 | + |
| 76 | + int number = 1234; |
| 77 | + |
| 78 | + //String numberString = String.valueOf(number); |
| 79 | + |
| 80 | + |
| 81 | + String numberString = Integer.toString(number); |
| 82 | + //print string value of number |
| 83 | + System.out.println("numberString: " + numberString); |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | +Output |
| 88 | +```java |
| 89 | +numberString: 1234 |
| 90 | +``` |
| 91 | +If you found this article worth, support me by [giving a cup of Coffee ☕](https://www.paypal.me/GauravKukade) |
| 92 | + |
| 93 | +### Related Articles |
| 94 | + |
| 95 | +- [How to convert String to Integer in Java](https://coderolls.com/convert-string-to-int/) |
| 96 | +- [Learn About Java String Pool And intern() Method](https://coderolls.com/java-string-pool-and-intern-method/) |
| 97 | +- [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/) |
| 98 | +- [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/) |
| 99 | +- [Compare Two Strings Lexicographically In Java](https://coderolls.com/compare-two-strings-lexicographically-in-java/) |
| 100 | +- [Difference Between StringBuffer and StringBuilder class](https://coderolls.com/difference-between-stringbuffer-and-stringbuilder/) |
| 101 | +- [8 Basic GIT Commands Every Newbie Developer Must Know](https://coderolls.com/basic-git-commands/) |
0 commit comments