Conversion of int to String

hi,
Do someone know which way of converting the int to String is better? In term of performance and other issues in the handset:
1. int a = 5;
String b = a + "";
2. int a = 5;
String b = String.valueOf(a);
Thank you!

Hi,
The second method is better. Because for the first method 3 String objects will be created.

Similar Messages

  • Conversion from int to String

    How can i convert an int to a String.
    There is no direct String constructor that take only an integer.
    Thanks

    int num = 1;
    String str = "" + num;
    or
    int num = 1;
    String str = Integer.toString(num);

  • Ints to Strings

    Can someone please tell me how I can convert an 'int' into a 'String'??
    The problem i am having is that i want to use some 'int' values and display them in a GUI TextField but I cant coz they are ints and a TextField only accepts Strings.
    So i want to be able to use the setText( ) method to do this after the conversion.
    Thanx.
    [K]

    then you look up javadocs,
    Integer:
    valueOf
    public static Integer valueOf(String s)
    throws NumberFormatException
    Returns an Integer object holding the value of the specified String. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) method. The result is an Integer object that represents the integer value specified by the string.
    In other words, this method returns an Integer object equal to the value of:
    new Integer(Integer.parseInt(s))
    Parameters:
    s - the string to be parsed. Returns:
    an Integer object holding the value represented by the string argument. Throws:
    NumberFormatException - if the string cannot be parsed as an integer.
    intValue
    public int intValue()
    Returns the value of this Integer as an int.
    Specified by:
    intValue in class Number
    Returns:
    the numeric value represented by this object after conversion to type int.
    String:
    suggestions above.

  • String to Int and Int to String

    How can I convert a string to Int & and an Int to String ?
    Say I've
    String abc="234"
    and I want the "int" value of "abc", how do I do it ?
    Pl. help.

    String.valueOf(int) takes an int and returns a string.
    Integer.parseInt(str) takes a string, returns an int.
    For all the others long, double, hex etc. RTFM :)

  • Converting int to String

    How can we convert from int to String?
    Ex:
    int i = 5;
    Then i need that 5 in String format....
    Message was edited by:
    kiran_panga

    String.valueOf(i)
    According to http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
    public static String valueOf(int i)
    Returns the string representation of the int argument.

  • Conversion from document to string

    I want to perform conversion from document to string for tht purpose i m doing sax parser but in the end i m getting null.The same thing if i do with DOM parser its working fine but some problem with sax parser.
    I am attaching the code if anyone can find the problem it would be gr8.
    Thanks in advance.
    public String DocumentToString(Document doc) {
              StreamResult result = null;
              try {
              SAXParserFactory SAXpf = SAXParserFactory.newInstance();     
    SAXParser SAXparser = SAXpf.newSAXParser();
              XMLr = SAXparser.getXMLReader();
         Source sXML = new SAXSource((InputSource) doc);
    result = new StreamResult(new StringWriter());
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(sXML, result);
    catch (TransformerConfigurationException e) {
    e.printStackTrace();
    catch (TransformerException e) {
    e.printStackTrace();
    catch(Exception e)
         System.out.println(e);
    return result.getWriter().toString();
    }

    Paddy,
    There are a couple of ways to create a Word file.  One is to use the Report Generation Toolkit which includes vi's to create and edit Word documents.  Since you are associated with a university you may already have the toolkit.  The other way is to use ActiveX.  You should be able to find examples of both in the forums.  There may also be an example that shipped with LV. 
    Here is one example to get you going http://zone.ni.com/devzone/cda/epd/p/id/992

  • Convert from int to String

    Hi, may i know how to convert from int to String??
    For instance, i've>>
    int i = 0;
    i++;
    String temp = i.toString();
    [Need to convert the int to String here. Am i doing the convertion correctly?? Pls correct me. Thanks!]

    Hi, may i know how to convert from int to String??
    For instance, i've>>
    int i = 0;
    i++;
    String temp = i.toString();
    [Need to convert the int to String here. Am i doing
    the convertion correctly?? Pls correct me. Thanks!]String temp = "" + i;

  • How to convert from int to string

    Can anyone help me on how to convert int to string?
    Thanks,
    Q

    int i = 3
    String S = i + ""
    i will be promoted to String automatically when the above expression is evaluated

  • Convert an int in string

    I wrote this code to convert an int in string but is failed.
    Someone might help me. thanks
    String UID="select max(right(CodiceUtente,4)) from Utenti";
         ResultSet rs=st.executeQuery(UID);
         int massimo=rs.getInt("CodiceUtente") + 1;
              //massimo deve essere trasformato in una stringa. Come si fa?
         String maxim =toString();
    my email is [email protected]

    Try this one: String maxim = toString(massimo).
    Find out more from the API (hope it accepts this long
    URL that should point directly to the right place) :
    http://www.ttu.ee/it/vorgutarkvara/wai4040/jdkdoc/jdk1.
    2.2/docs/api/java/lang/String.html#toString()

  • Checking wheter is "int" or "String"

    Hi..
    I would like to know is there a way to know if an attribute type is int or String while executing my application.
    I heard "why do you wanna know?", that is just because this attribute belongs to another class and I'm gonna use it in every class. Later it's possible this attribute will change it's type so I would provide some if statements now, for don't have trouble with it later.
    Thanks in advance.

    Hi..
    I would like to know is there a way to know if an
    attribute type is int or String while executing my
    application.
    I heard "why do you wanna know?", that is just because
    this attribute belongs to another class and I'm gonna
    use it in every class. Later it's possible this
    attribute will change it's type so I would provide
    some if statements now, for don't have trouble with it
    later.
    Thanks in advance.What do you mean by "attribute type"?
    How will it change its type? - if you change a variable type from int to String or vice versa, then code that uses it won't compile anyway.
    Are you doing something unusual with reflection? If you are, could you post some code that demostrates what you actually want to do?

  • Conversion of Double to String

    Is there any method in JAVA which can convert a double value to String (but expand it)
    for ex: I want to convert 1.2E7 to look like "12000000"
    I know an equal C conversion technique which is as follows => sprintf(strAmount, "%35.2lf", fAmount);
    Plz.. Reply to:
    [email protected]

    i have tried the DecimalFormat in the reply, but the d
    in the fd.format(d) is still in xxxxxxxxEx format. And
    i couldn't perform any calculation based on the
    converted value with this format after saving this
    value to may be database or some other files. Can you
    advice how to get 170000000.00 instead of
    1.70000000E8.Use the setMaximumIntegerDigits() method of the NumberFormat.
    Servus.

  • Conversion of XSTRING to String is incomplete

    Hi ,
    I am trying to conver a Xstring to String. But the output string after conversion is incomplete.
    Here is the code snippet-
    CALL METHOD CL_ABAP_CONV_IN_CE=>CREATE
          EXPORTING
            INPUT       = l_xstring
            ENCODING    = 'UTF-8'
            REPLACEMENT = '?'
            IGNORE_CERR = ABAP_TRUE
          RECEIVING
            CONV        = loc_CONV.
            length = xstrlen( l_xstring ).
            CALL METHOD loc_CONV->READ
            EXPORTING
            N = length
            IMPORTING
                DATA = loc_string
                LEN = length.
    Can anyone please help?
    Best Regards,
    Lata

    Hi,
    http://www.sapfans.com/forums/viewtopic.php?p=403405&sid=e524cddae19e7ae48a12477e68c6032d
    Regards,
    Sravanthi

  • Converting int to string, adding to string then pointing to an url

    So I'm getting a null pointer exception here's a snippet of my code (theres plus signs on both sides of the a in my code):
    private Image[] tiles;
    public Map() {
    for(int a=0; a<9; a++){
    ImageIcon aa = new ImageIcon(this.getClass().getResource("mapsquares"+a+".png"));
    tiles[a] = aa.getImage();
    I've also tried:
    private Image[] tiles;
    public Map() {
    for(int a=0; a<9; a++){
    ImageIcon aa = new ImageIcon(this.getClass().getResource("mapsquares$s.png", String.valueOf(a)));
    tiles[a] = aa.getImage();
    whats wrong with either of these?
    Edited by: Wub on Feb 28, 2013 3:17 PM

    1.) You should use code tags when posting code.
    2.) You should read the stacktrace closely to find out which exact line number in your code threw the exception.
    My guess is that it's the line tiles[a] = aa.getImage(); because you didn't initialize the array tiles using the new operator. This is just a guess since you didn't post the full stacktrace. Also, NullpointerExceptions are one of the easiest to debug. Just adding print out lines at strategic values can show you which variables are initialized and which ones are not.

  • Int to string?

    I have an int that I would like to make to a String. How?
    Why dos'nt this work?
    int a = 5;
    String t = toString(a);

    Does the class this is in have a toString method? If not, that's why it doesn't work. The standard idiom for converting an int to a String is to say:
    String value = myInt + "";Lee

  • Int to string to string array

    Sorry, if my question is too wordy.
    First, the problem -- I am reading integers into my application and converting the those into strings using valueOf(). I then need to use those strings in a switch statment. The only way I could figure how to do this was to use the same String variable in each switch statement but change the the message to fit each case. My professor suggested I use an array list instead.
    Second, the question -- how can I convert the String I have generated into a String Array and use the indexing of the array to correspond to the specific text for each case?
    Thank you.

    indeed, you can't use the switch mechanism on strings.
    but, if the requirement is that you convert an integer into a string and then switch on those (I don't know why...) from an array of the values, swap the String value back into an int and switch from there.

Maybe you are looking for