Results 1 to 8 of 8
Thread: Change to string
- 04-12-2010, 10:02 PM #1
-
Java Code:String iString = String.valueOf(i);
- 04-13-2010, 03:05 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Or,
Java Code:String strString = Integer.toString(i);
- 04-13-2010, 07:41 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
or
Java Code:String string = i + ""
- 04-13-2010, 08:14 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,589
- Blog Entries
- 7
- Rep Power
- 17
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
- 04-14-2010, 02:30 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-14-2010, 10:13 AM #7
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.
- 04-14-2010, 03:26 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
change string to array of integer
By prof.deedee in forum New To JavaReplies: 4Last Post: 11-09-2009, 02:47 AM -
How to change Bubble+Selection+Insertion Sorts to Sort String Values?
By VinceGuad in forum New To JavaReplies: 3Last Post: 01-26-2009, 12:20 AM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
How to change string Color
By Java.child in forum AWT / SwingReplies: 3Last Post: 01-06-2009, 04:27 AM -
Cast Error Caught (change) Class is really: java.lang.String
By barney in forum Advanced JavaReplies: 1Last Post: 08-02-2007, 04:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks