?
Solved

convert int to string

Posted on 2002-04-02
14
Medium Priority
?
5,111 Views
Last Modified: 2008-01-09
what is the best way to convert int to string in java?
0
Comment
Question by:aussie_guy_nik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 19

Accepted Solution

by:
cheekycj earned 200 total points
ID: 6912899
Assuming i is your int var:
Integer.toString(i);

HTH,
CJ
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 6912906
Simple way.

int your_integer;
String int_str = new String(your_integer);
0
 
LVL 3

Expert Comment

by:saxaboo
ID: 6912909
int i = 567;
String s = Integer.toString(i);

-S
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6912922

I prefer String.valueOf(i).
Shortest way is ""+i
0
 

Expert Comment

by:0xDEADBEEF
ID: 6912951
int i = 29331;
String s = String.valueOf(i);
0
 

Expert Comment

by:0xDEADBEEF
ID: 6912957
Now that is what you get when you jam ur internet connection. Everybody's faster than you ;-)
0
 

Expert Comment

by:sunilvirus
ID: 6913149
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 6913756
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
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6914996
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
 

Expert Comment

by:0xDEADBEEF
ID: 6915252
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 6915605
aussie_guy_nik: any feedback.. I am pretty sure you have gotten a solution :-)
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6916176
One note:

do not use "" + i. This will work, but it is about 15 times slower than Integer.toString(i).
0
 
LVL 2

Expert Comment

by:vogen
ID: 6925675
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
 
LVL 19

Expert Comment

by:cheekycj
ID: 6944905
Thanx for the "A"
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question