Skip to content

Commit 933d3da

Browse files
committed
Add new blogpost, how to convert an int to string in java
1 parent 07e0c87 commit 933d3da

File tree

4 files changed

+105
-5
lines changed

4 files changed

+105
-5
lines changed

‎_posts/java-string/2020-01-05-convert-string-to-int.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ I have listed down two ways to convert a String into an Integer.
2424

2525
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).
2626

27-
[![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")
27+
<iframe width="560" height="315" src="https://www.youtube.com/embed/pNHjvpNHVCs" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
2828

2929
Now we will see the above ways to convert string to integer one by one.
3030

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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/)

‎_posts/java-uuid/2020-12-23-create-uuid-in-java.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: post
33
title: "How To Create UUID in Java?"
44
author: gaurav
55
image: assets/images/2020-12-23/create-uuid-in-java.webp
6-
categories: [ Java, Core Java, String]
6+
categories: [Java, Core Java, String]
77
description: "In this article you will see, how to create UUID in Java."
88
featured: true
99
hidden: true
@@ -79,6 +79,5 @@ If you found this article worth, support me by [giving a cup of Coffee ☕](htt
7979
- [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/)
8080
- [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/)
8181
- [Compare Two Strings Lexicographically In Java](https://coderolls.com/compare-two-strings-lexicographically-in-java/)
82-
- [Difference Between StringBuffer and StringBuilder class](https://coderolls.com/difference-between-stringbuffer-and-stringbuilder/)
83-
84-
- [8 Basic GIT Commands Every Newbie Developer Must Know](https://coderolls.com/basic-git-commands/)
82+
- [Difference Between StringBuffer and StringBuilder class](https://coderolls.com/difference-between-stringbuffer-and-stringbuilder/)
83+
- [8 Basic GIT Commands Every Newbie Developer Must Know](https://coderolls.com/basic-git-commands/)
Binary file not shown.

0 commit comments

Comments
 (0)