JTextField with int value

Discussion in 'Java' started by shoa, Jul 8, 2005.

  1. shoa

    shoa Guest

    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
    shoa, Jul 8, 2005
    #1
    1. Advertising

  2. shoa wrote:
    > 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


    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
    Thomas Weidenfeller, Jul 8, 2005
    #2
    1. Advertising

  3. shoa

    Roland Guest

    On 8-7-2005 9:39, shoa wrote:
    > 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_/ /__/
    / \ /_/ / \
    Roland, Jul 8, 2005
    #3
  4. shoa

    shoa Guest

    Thanks all

    So, may be the simple way is:

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


    "Roland" <> wrote in message
    news:42ce2ee9$0$93731$4all.nl...
    > On 8-7-2005 9:39, shoa wrote:
    > > 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_/ /__/
    > / \ /_/ / \
    shoa, Jul 8, 2005
    #4
  5. shoa wrote:
    > 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
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
    Thomas Weidenfeller, Jul 8, 2005
    #5
  6. "shoa" <> writes:

    > 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.
    Tor Iver Wilhelmsen, Jul 8, 2005
    #6
  7. shoa

    shoa Guest

    Thanks all
    That helps me understand clearly ...



    "Thomas Weidenfeller" <> wrote in message
    news:dam3uv$10t$...
    > shoa wrote:
    > > 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
    > --
    > The comp.lang.java.gui FAQ:
    > ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    > http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
    shoa, Jul 9, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,626
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    450
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    943
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    429
    James Kanze
    Apr 17, 2007
Loading...

Share This Page