wood burning stoves*
The moose likes Beginning Java and the fly likes Convert int to a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert int to a string" Watch "Convert int to a string" New topic
Author

Convert int to a string

Sarone Thach
Ranch Hand

Joined: Jun 25, 2003
Posts: 89
Hey All,
don't know how difficult this question is, so don't shoot me if it is.
I was wondering if this code is any good.
JTextField txtField = new JTextField();
int somenumber = 10;
txtField.setText(somenumber + "");
//or should i do this
txtField.setText(new Integer(somenumber).toString());
which is better. If none of the above, what is better?
thanks,
Sarone
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy,
How about something like:

t.setText("" + 10);
(unless you need the '10' to be a variable... in which case your "" + someNumber thing would work).
Your bottom example just has a bit more overhead... here's one way to find out:
After you compile a class, you can say at the command-line:
javap -c MyClass
(whatever your class name is)
and you'll get a look at the bytecode generated by the compiler. Now, you do NOT need to know what these mean!! But you can get an idea by the number of lines generated as to how efficient your code is. So, when you run javap on :
public class TestGUI {
public static void main (String [] args) {
JTextField t = new JTextField();
t.setText("" + 10);
}
}
You get:

Method void main(java.lang.String[])
0 new #2 <Class javax.swing.JTextField>
3 dup
4 invokespecial #3 <Method javax.swing.JTextField()>
7 astore_1
8 aload_1
9 ldc #4 <String "10">
11 invokevirtual #5 <Method void setText(java.lang.String)>
14 return

// actually you get a little more for the class itself, but we're only concerned about main() here.
But if you run javap on:
public static void main (String [] args) {
JTextField t = new JTextField();
t.setText(new Integer(10).toString());
}
You get:
Method void main(java.lang.String[])
0 new #2 <Class javax.swing.JTextField>
3 dup
4 invokespecial #3 <Method javax.swing.JTextField()>
7 astore_1
8 aload_1
9 new #4 <Class java.lang.Integer>
12 dup
13 bipush 10
15 invokespecial #5 <Method java.lang.Integer(int)>
18 invokevirtual #6 <Method java.lang.String toString()>
21 invokevirtual #7 <Method void setText(java.lang.String)>
24 return

So... without knowing or caring (for now) what these mean, you can see that the second one had many more lines than the first! Plus, the second one generated an additional object which, although it can be garbage collected, still adds to the overhead of your program at runtime.
Now, in a Swing app, your most important goal is probably something OTHER than pure efficiency... so be sure to always look for things like 'extensibility', 'flexibility', and 'maintainability' rather than just simply counting the lines of bytecode to determine how to write your code -- but if you are choosing between two approaches, and they both are equal from the standpoint of readability and maintenance, this can help you look at the efficiency differences. But you can probably already tell, just by looking at the code that you're making more objects than you need, etc...
cheers,
Kathy
p.s. don't freak out looking at the byte codes! You can live your entire Java life and write perfectly good code without ever learning -- or looking at -- the byte code. It's just a fun exercise sometimes. You know, when you have absolutely NOTHING else to do.
"If you want to learn Java... dive in, Head First"
-Scott McNealy, on "Head First Java"
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Given an int variable i, I would use the static method String.valueOf(i). I think this is generally considered to be the best solution.


[Jess in Action][AskingGoodQuestions]
Sarone Thach
Ranch Hand

Joined: Jun 25, 2003
Posts: 89
Ernest, thank you so much, that's exactly what I'm looking for, i do lots of conversions from int to String - you have no idea how relieved i am.
Kathy, what a great suggestion. I never knew i could do that. I will do it for other code i have doubts with.
Thanks to both of you for your time.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Convert int to a string