user595693
user595693

Reputation:

How to convert an integer value to string?

How do I convert an integer variable to a string variable in Java?

Upvotes: 11

Views: 93790

Answers (5)

Hoque MD Zahidul
Hoque MD Zahidul

Reputation: 11949

There are many different type of wat to convert Integer value to string

 // for example i =10

  1) String.valueOf(i);//Now it will return "10"  

  2 String s=Integer.toString(i);//Now it will return "10" 

  3) StringBuilder string = string.append(i).toString();

   //i = any integer nuber

  4) String string = "" + i; 

  5)  StringBuilder string = string.append(i).toString();

  6) String million = String.format("%d", 1000000)

Upvotes: 0

Jegan
Jegan

Reputation: 139

Here is the method manually convert the int to String value.Anyone correct me if i did wrong.

/**
 * @param a
 * @return
 */
private String convertToString(int a) {

    int c;
    char m;
    StringBuilder ans = new StringBuilder();
    // convert the String to int
    while (a > 0) {
        c = a % 10;
        a = a / 10;
        m = (char) ('0' + c);
        ans.append(m);
    }
    return ans.reverse().toString();
}

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838146

There are at least three ways to do it. Two have already been pointed out:

String s = String.valueOf(i);

String s = Integer.toString(i);

Another more concise way is:

String s = "" + i;

See it working online: ideone

This is particularly useful if the reason you are converting the integer to a string is in order to concatenate it to another string, as it means you can omit the explicit conversion:

System.out.println("The value of i is: " + i);

Upvotes: 10

Nanne
Nanne

Reputation: 64399

  Integer yourInt;
  yourInt = 3;
  String yourString = yourInt.toString();

Upvotes: 0

Shijilal
Shijilal

Reputation: 2169

you can either use

String.valueOf(intVarable)

or

Integer.toString(intVarable)

Upvotes: 28

Related Questions