Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Add new blogpost, how to convert an int to string in java #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _posts/java-string/2020-01-05-convert-string-to-int.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
<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>

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

Expand Down
101 changes: 101 additions & 0 deletions _posts/java-string/2021-02-27-convert-int-to-string.md
Original file line number Diff line number Diff line change
@@ -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/)
7 changes: 3 additions & 4 deletions _posts/java-uuid/2020-12-23-create-uuid-in-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/)
Binary file not shown.