convert int to string

what is the best way to convert int to string in java?
aussie_guy_nikAsked:
Who is Participating?
 
cheekycjConnect With a Mentor Commented:
Assuming i is your int var:
Integer.toString(i);

HTH,
CJ
0
 
Peter KwanAnalyst ProgrammerCommented:
Simple way.

int your_integer;
String int_str = new String(your_integer);
0
 
saxabooCommented:
int i = 567;
String s = Integer.toString(i);

-S
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Igor BazarnyCommented:

I prefer String.valueOf(i).
Shortest way is ""+i
0
 
0xDEADBEEFCommented:
int i = 29331;
String s = String.valueOf(i);
0
 
0xDEADBEEFCommented:
Now that is what you get when you jam ur internet connection. Everybody's faster than you ;-)
0
 
sunilvirusCommented:
if your purpose is to print the integer you can directly write(assuming i as int)
System.out.println("the number is "+i);

but if you really want to convert it to string you write
String s=new String(Integer.toString(i));
0
 
cheekycjCommented:
Integer.toString(i) should return a new string.. there is no need to instantiate a new String object.  AFAIK, Strings are immutable, right?

I guess I had the fastest internet connection ;-)

CJ
0
 
shyamkumarreddyCommented:
Yes CJ is right.
Strings are not immutable.

In all the cases specified above have to return a String object right.

Say Integer.toString(i) or String.valueOf(). What is the difference then.
Do anybody find difference in this.

Shyam
0
 
0xDEADBEEFCommented:
None actually. That is, Integer.toString(int) is slightly faster. String.valueof(int) looks like this:

public string valueOf(int i){
  return Integer.toString(i, 10);
}

where Integer.toString(int, int) checks if 10 is greater than Character.MAX_RADIX or lower than Character.MIN_RADIX (it usually isn't ;) ), checks whether 10 equals 10 (it usually does...) and delegates to Integer.toString(int):

public String toString(int i, int radix){
 
  if (radix < Character.MIN_RADIX || radix >Character.MAX_RADIX)
  radix = 10;

  /* Use the faster version */
  if (radix == 10) {
    return toString(i);
  }

  //...

}

This is taken from java/lang/String.java and java/lang/Integer.java included in the src.zip file that comes with the JDK.
0
 
cheekycjCommented:
aussie_guy_nik: any feedback.. I am pretty sure you have gotten a solution :-)
0
 
m_onkey_boyCommented:
One note:

do not use "" + i. This will work, but it is about 15 times slower than Integer.toString(i).
0
 
vogenCommented:
the best way to convert integer to string is using
toString() method of INTEGER Class
you can have a look methods of INTEGER Class in API documents.

where you can see this method:
public static String toString(int i)


kool!! mate
0
 
cheekycjCommented:
Thanx for the "A"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.