Can I change i to a string? if yes how? :OCode:for (int i = 1; i <= 31; i++) {
makePanel(i);
}
Printable View
Can I change i to a string? if yes how? :OCode:for (int i = 1; i <= 31; i++) {
makePanel(i);
}
Code:String iString = String.valueOf(i);
Or,
Code:String strString = Integer.toString(i);
orCode: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
you're right. consulting my "Effective Java Programming Language Guide" from J. Bloch he says
Quote:
To achieve acceptable performance, use a StringBuffer in place of a String to store the statement under construction:
and suggests this code
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.
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.