• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting TextField value  RSS feed

 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a long value and i have to set the value to a text field.
So what i did is :
long value = 10;
textField.setText(new Long(value).toString()));
Also i have way to do this :
textField.setText(value+"");

I want to understand which is the best way to do and why?
Can somebody help me understanding this.

Thanks in advance.
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using 'String.valueOf(value)' - the String.valueOf is actually several methods, converting all sorts of simple data types and objects to their String representation.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Long.toString is implemented as



Your alternative

value + ""

is compiled to something like

new StringBuffer().append(value).append("").toString()

where append(value) basically calls String.valueOf(value), again.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Adne - String.valueOf(value) is more efficient; with new Long(value).toString() you are creating an unnecessary temporary object (a Long), and value + "" as Ilja explains it also creates an unnecessary temp object (StringBuffer) and even does some more unnecessary work (appending "").
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!