JTextField with int value

S

shoa

Hello

I have a object (myObject) with a value (ID) that is the "int" type. I have
to display this value in a jTextField (an extends of JTextField). As the
function setText in jTextField requires a String value, I have to add a
string "" into function setText as the following:

jTextField.setText (myOjbect.getObjectID() + "" );


Is there any better way to do so that I do not have to add "" string?

Thank you
S.Hoa
 
T

Thomas Weidenfeller

shoa said:
I have a object (myObject) with a value (ID) that is the "int" type. I have
to display this value in a jTextField (an extends of JTextField).

jTextField is a strange name for a class. It violates the common Java
naming convention.
Is there any better way to do so that I do not have to add "" string?

Integer.toString().

/Thomas
 
R

Roland

Hello

I have a object (myObject) with a value (ID) that is the "int" type. I have
to display this value in a jTextField (an extends of JTextField). As the
function setText in jTextField requires a String value, I have to add a
string "" into function setText as the following:

jTextField.setText (myOjbect.getObjectID() + "" );

jTextField.setText( Integer.toString( myOjbect.getObjectID() ) );

or

jTextField.setText( String.valueOf( myOjbect.getObjectID() ) );

Sun's implemenatation of String.valueOf is slightly slower, as it calls
Integer.toString(i,10) which eventually calls Integer.toString(i).
Is there any better way to do so that I do not have to add "" string?

Thank you
S.Hoa
--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
 
S

shoa

Thanks all

So, may be the simple way is:

jTextField.setText (myOjbect.getObjectID() + "" );
 
T

Thomas Weidenfeller

shoa said:
So, may be the simple way is:

jTextField.setText (myOjbect.getObjectID() + "" );

Congratulations, you selected the most ugly and CPU wasting version.

Unless the compiler or JIT manages to optimize it, it burns the most CPU
cycles. 1.4 instantiates a StringBuffer, uses two calls to
StringBuffer.append(), and one to StringBuffer.toString() to convert to
the string. While 1.5 uses an instance of StringBuilder, two calls to
StringBuilder.append() and one to StringBuilder.toString() to do the
conversion.

The append(int) calls will internally use something like
Integer.toString() probably even via String.valueOf(). So you got
nothing but extra method calls.

/Thomas
 
T

Tor Iver Wilhelmsen

shoa said:
jTextField.setText (myOjbect.getObjectID() + "" );

No, that's equivalent to

jTextField.setText (new StringBuffer().append(myOjbect.getObjectID())
.append("").toString() );

which is slower than any of the alternatives.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top