diff --git a/_posts/java-string/2020-01-05-convert-string-to-int.md b/_posts/java-string/2020-01-05-convert-string-to-int.md index 13594c5..a881bb6 100644 --- a/_posts/java-string/2020-01-05-convert-string-to-int.md +++ b/_posts/java-string/2020-01-05-convert-string-to-int.md @@ -24,7 +24,7 @@ I have listed down two ways to convert a String into an Integer. You can watch [the video on my youtube channel for "How To Convert A String To An Integer In Java"](https://www.youtube.com/watch?v=pNHjvpNHVCs). -[![How To Convert A String To An Integer In Java](https://img.youtube.com/vi/pNHjvpNHVCs/0.jpg)](https://www.youtube.com/watch?v=pNHjvpNHVCs "How To Convert A String To An Integer In Java") + Now we will see the above ways to convert string to integer one by one. diff --git a/_posts/java-string/2021-02-27-convert-int-to-string.md b/_posts/java-string/2021-02-27-convert-int-to-string.md new file mode 100644 index 0000000..b8c3515 --- /dev/null +++ b/_posts/java-string/2021-02-27-convert-int-to-string.md @@ -0,0 +1,101 @@ +--- +layout: post +title: "How To Convert An Integer To String In Java" +author: gaurav +image: assets/images/2021-02-27/convert-int-to-string.webp +categories: [Java, Core Java, String] +description: "In this article you are going to learn how can you convert an integer to a String." +--- + +In this article you are going to learn how can you convert an integer to a String. + +There are multiple ways we can convert an int to string in java. let's check what are they. + +1. `String.ValueOf()` method +2. `Integer.toString()` method +3. String.format() method + +## 1. `String.valueOf()` method + +`String.valueOf()` method returns the string representation of the `Object` argument. + +So when we pass an int as parameter to `valueOf()` method, it returns it's String representation. + +If the argument is `null`, then a string equal to `"null"`will be returned. + +Internally `valueOf()` method calls `toString()` method on the object argument to get its string representation. + +An example to convert an int to string using `valueOf()` method is given below. + +```java +/* + * A program to convert an int to string in java using + * the String.toSTring() method. + * + * @author Gaurav Kukade at coderolls.com + */ + +public class IntToSTring{ + + public static void main(String []args){ + + int number = 1234; + + String numberString = String.valueOf(number); + + //print string value of number + System.out.println("numberString: " + numberString); + } +} +``` +Output +```java +numberString: 1234 +``` + +## 2. `Integer.toString()` method + +`Integer.toString()` method returns the String object of the Integer's value. + +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. + +An example to convert an int to string using the `toString()` method is given below. + +```java +/* + * A program to convert an int to string in java using + * the Integer.toString() method. + * + * @author Gaurav Kukade at coderolls.com + */ + +public class IntToSTring{ + + public static void main(String []args){ + + int number = 1234; + + //String numberString = String.valueOf(number); + + + String numberString = Integer.toString(number); + //print string value of number + System.out.println("numberString: " + numberString); + } +} +``` +Output +```java +numberString: 1234 +``` +If you found this article worth, support me by [giving a cup of Coffee ☕](https://www.paypal.me/GauravKukade) + +### Related Articles + +- [How to convert String to Integer in Java](https://coderolls.com/convert-string-to-int/) +- [Learn About Java String Pool And intern() Method](https://coderolls.com/java-string-pool-and-intern-method/) +- [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/) +- [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/) +- [Compare Two Strings Lexicographically In Java](https://coderolls.com/compare-two-strings-lexicographically-in-java/) +- [Difference Between StringBuffer and StringBuilder class](https://coderolls.com/difference-between-stringbuffer-and-stringbuilder/) +- [8 Basic GIT Commands Every Newbie Developer Must Know](https://coderolls.com/basic-git-commands/) diff --git a/_posts/java-uuid/2020-12-23-create-uuid-in-java.md b/_posts/java-uuid/2020-12-23-create-uuid-in-java.md index 2ea991f..959af80 100644 --- a/_posts/java-uuid/2020-12-23-create-uuid-in-java.md +++ b/_posts/java-uuid/2020-12-23-create-uuid-in-java.md @@ -3,7 +3,7 @@ layout: post title: "How To Create UUID in Java?" author: gaurav image: assets/images/2020-12-23/create-uuid-in-java.webp -categories: [ Java, Core Java, String] +categories: [Java, Core Java, String] description: "In this article you will see, how to create UUID in Java." featured: true hidden: true @@ -79,6 +79,5 @@ If you found this article worth, support me by [giving a cup of Coffee ☕](htt - [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/) - [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/) - [Compare Two Strings Lexicographically In Java](https://coderolls.com/compare-two-strings-lexicographically-in-java/) -- [Difference Between StringBuffer and StringBuilder class](https://coderolls.com/difference-between-stringbuffer-and-stringbuilder/) - -- [8 Basic GIT Commands Every Newbie Developer Must Know](https://coderolls.com/basic-git-commands/) +- [Difference Between StringBuffer and StringBuilder class](https://coderolls.com/difference-between-stringbuffer-and-stringbuilder/) +- [8 Basic GIT Commands Every Newbie Developer Must Know](https://coderolls.com/basic-git-commands/) diff --git a/assets/images/2021-02-27/convert-int-to-string.webp b/assets/images/2021-02-27/convert-int-to-string.webp new file mode 100644 index 0000000..dfd2bb5 Binary files /dev/null and b/assets/images/2021-02-27/convert-int-to-string.webp differ