Int to String in Java – How to Convert an Integer into a String
source link: https://www.freecodecamp.org/news/how-to-convert-integer-to-string-in-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
January 5, 2023 / #Java
Int to String in Java – How to Convert an Integer into a String
You can convert variables from one data type to another in Java using different methods.
In this article, you'll learn how to convert integers to strings in Java in the following ways:
- Using the
Integer.toString()
method. - Using the
String.valueOf()
method. - Using the
String.format()
method. - Using the
DecimalFormat
class.
How to Convert an Integer to a String in Java Using Integer.toString()
The Integer.toString()
method takes in the integer to be converted as a parameter. Here's what the syntax looks like:
Integer.toString(INTEGER_VARIABLE)
Here's an example:
class IntToStr {
public static void main(String[] args) {
int age = 2;
String AGE_AS_STRING = Integer.toString(age);
System.out.println("The child is " + AGE_AS_STRING + " years old");
// The child is 2 years old
}
}
In the example above, we created an integer – age
– and assigned a value of 2 to it.
To convert the age
variable to a string, we passed it as a parameter to the Integer.toString()
method: Integer.toString(age)
.
We stored this new string value in a string variable called AGE_AS_STRING
.
We then concatenated the new string variable with other strings: "The child is " + AGE_AS_STRING + " years old"
.
But, would an error be raised if we just concatenated the age variable to these other strings without any sort of conversion?
class IntToStr {
public static void main(String[] args) {
int age = 2;
System.out.println("The child is " + age + " years old");
// The child is 2 years old
}
}
The output above is the same as the example where we had to convert the integer to a string.
So how do we know if the type conversion actually worked?
We can check variable types using the Java getClass()
object. That is:
class IntToStr {
public static void main(String[] args) {
int age = 2;
String AGE_AS_STRING = Integer.toString(age);
System.out.println(((Object)age).getClass().getSimpleName());
// Integer
System.out.println(AGE_AS_STRING.getClass().getSimpleName());
// String
}
}
Now we can verify that when the age
variable was created, it was an Integer
, and after type conversion, it became a String
.
How to Convert an Integer to a String in Java Using String.valueOf()
The String.valueOf()
method also takes the variable to be converted to a string as its parameter.
class IntToStr {
public static void main(String[] args) {
int age = 2;
String AGE_AS_STRING = String.valueOf(age);
System.out.println("The child is " + AGE_AS_STRING + " years old");
// The child is 2 years old
}
}
The code above is similar to that in the last section:
- We created an integer called
age
. - We passed the
age
integer as a parameter to theString.valueOf()
method:String.valueOf(age)
.
You can also check to see if the type conversion worked using the getClass()
object:
System.out.println(((Object)age).getClass().getSimpleName());
// Integer
System.out.println(AGE_AS_STRING.getClass().getSimpleName());
// String
How to Convert an Integer to a String in Java Using String.format()
The String.format()
method takes in two parameters: a format specifier and the variable to be formatted.
Here's an example:
class IntToStr {
public static void main(String[] args) {
int age = 2;
String AGE_AS_STRING = String.format("%d", age);
System.out.println("The child is " + AGE_AS_STRING + " years old");
// The child is 2 years old
}
}
In the example above, we passed in two parameters to the String.format()
method: "%d"
and age
.
"%d"
is a format specifier which denotes that the variable to be formatted is an integer.
age
, which is the second parameter, will be converted to a string and stored in the AGE_AS_STRING
variable.
You can also check the variable types before and after conversion:
System.out.println(((Object)age).getClass().getSimpleName());
// Integer
System.out.println(AGE_AS_STRING.getClass().getSimpleName());
// String
How to Convert an Integer to a String in Java Using DecimalFormat
The DecimalFormat
class is used for formatting decimal numbers in Java. You can use it in different ways, but we'll be using it to convert an integer to a string.
Here's an example:
import java.text.DecimalFormat;
class IntToStr {
public static void main(String[] args) {
int age = 2;
DecimalFormat DFormat = new DecimalFormat("#");
String AGE_AS_STRING = DFormat.format(age);
System.out.println("The child is " + AGE_AS_STRING + " years old");
// The child is 2 years old
System.out.println(((Object)age).getClass().getSimpleName());
// Integer
System.out.println(AGE_AS_STRING.getClass().getSimpleName());
// String
}
}
Let's break the code down:
- To be able to use the
DecimalFormat
class in the example above, we imported it:import java.text.DecimalFormat;
. - We created the integer
age
variable. - We then created a new object of the
DecimalFormat
class calledDFormat
. - Using the object's
format()
method, we convertedage
to a string:DFormat.format(age);
.
Summary
In this article, we talked about converting integers to strings in Java.
We saw examples that showed how to use three different methods – Integer.toString()
, String.valueOf()
, String.format()
— and the DecimalFormat
class to convert variables from integers to strings.
Each example showed how to check the data type of a variable before and after conversion.
Happy coding!
ihechikara.com
If you read this far, tweet to the author to show them you care.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK