Results 1 to 8 of 8
  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Change to string

    PHP Code:
    for (int i = 1; i <= 31; i++) {
           makePanel(i);
    }
    Can I change i to a string? if yes how? :O

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    or
    Java Code:
    String string = i + ""

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by senorbum View Post
    or
    Java Code:
    String string = i + ""
    You may not realize it but that is quite an expensive way of obtaining the String equivalent of an int: the compiler creates a StringBuffer for you, appends both an empty String to it as well as the int which will be converted to a String first. Better use the String.valueOf( ... ) method.

    kind regards,

    Jos

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by senorbum View Post
    or
    Java Code:
    String string = i + ""
    For this is not a good approach, as JosAH says. Creation of additions resources in the code is not a good practice at all. Better to try avoid addition stuff as much as possible.

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Eranga View Post
    Better to try avoid addition stuff as much as possible. [/COLOR]

    you're right. consulting my "Effective Java Programming Language Guide" from J. Bloch he says

    To achieve acceptable performance, use a StringBuffer in place of a String to store the statement under construction:

    and suggests this code


    Java Code:
    public String statement() {
    StringBuffer s = new StringBuffer(numItems() * LINE_WIDTH);
    for (int i = 0; i < numItems(); i++)
    s.append(lineForItem(i));
    return s.toString();
    }

    for string concatenation.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What are the tricky points you seen in that above code lol. I mean that I wonder is there any special reason to use separate/multiple methods to get values.

Similar Threads

  1. change string to array of integer
    By prof.deedee in forum New To Java
    Replies: 4
    Last Post: 11-09-2009, 02:47 AM
  2. Replies: 3
    Last Post: 01-26-2009, 12:20 AM
  3. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  4. How to change string Color
    By Java.child in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2009, 04:27 AM
  5. Replies: 1
    Last Post: 08-02-2007, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •