Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, December 17, 2019

Java Program to Convert Int to String (Best Way)

1. Overview


In this conversion series articles, we will learn today how to convert int to String in java. Previously, I explained converting String to Int in java.

We do not talk much about unnecessary things. Directly jumping into our topic.

int conversion into String can be done using following java builtin methods.

String.valueOf()
Integer.toString()
String.format() 
Using + operator

Java Program to Convert Int to String



2. Program to convert int to string using String.valueOf()


String class a static utility method valueOf() and it is overloaded. It takes an int as an argument and returns a String value for the provided int.

package com.javaprogramto.engineering.programs.conversions;

/**
 * 
 * Java Convert - Int to String
 * 
 * @author javaprogramto.com
 *
 */
public class IntToString {

    public static void main(String[] args) {

        int value = 100;
        String output = "";

        // Way 1: String.valueOf()
        output = String.valueOf(value);
        System.out.println("String.value() : " + output);

    }
}

Output:

String.value() : 100

3. Program to convert int to a string using Integer.toString()


Integer class has a static toString() method and takes integer argument.


// Way 2: Integer.toString()
int value2 = 200;
String output2 = Integer.toString(value2);
System.out.println("Integer.toString() : " + output2);

Output:

Integer.toString() : 200

You might have a question which one better in these two approaches either String.valueOf() or Integer.toString()? For better understanding, see the below internal code for String.valueOf().

String.valueOf(int i) internal code:


    public static String valueOf(int i) {
        return Integer.toString(i);
    }

valueOf() internally calls the Integer.toString() method. So, it is recommended to use Integer.toString() method but you are a fan of String, use the String valueOf() method because it is overloaded and provides support for converting to other types such as char, float, double into String.

4. Program to convert int to a string using String.format() 

The String also has another static method format() which takes two arguments such as type and value.

int value3 = 300;
String output3 = String.format("%d", value3);
System.out.println("String.format() : " + output3);

Output:

String.format() : 300

5. Program to convert int to a string using '+' operator


First, create an empty string and concat int value using '+' operator. This produces a String with int value.

int value4 = 400;
String output4 = "" + value4;
System.out.println("+ operator : " + output4);

Output:

+ operator : 400


6. Java Program to Convert Int to String - Full Example Program


Showing all 4 ways in a single program.


package com.javaprogramto.engineering.programs.conversions;

/**
 * 
 * Java Convert - Int to String
 * 
 * @author javaprogramto.com
 *
 */
public class IntToString {

    public static void main(String[] args) {

        int value = 100;
        String output = "";

        // Way 1: String.valueOf()
        output = String.valueOf(value);
        System.out.println("String.value() : " + output);

        // Way 2: Integer.toString()
        int value2 = 200;
        String output2 = Integer.toString(value2);
        System.out.println("Integer.toString() : " + output2);

        // Way 3: String.format()
        int value3 = 300;
        String output3 = String.format("%d", value3);
        System.out.println("String.format() : " + output3);

        // Way 4: '+' operator
        int value4 = 400;
        String output4 = "" + value4;
        System.out.println("+ operator : " + output4);
    }
}

Output:

String.value() : 100
Integer.toString() : 200
String.format() : 300
+ operator : 400

7. Conclusion


In this article, We've seen what are the inbuilt methods in java to convert int to String. Shown example programs using 3 api methods and '+' operator.

No comments:

Post a Comment

Please do not add any spam links in the comments section.