Sometimes there is need to change an integer to text. I often use the following way:
"" + myNumber
But there is alternative way:
Integer.toString(myNumber)
Which one is better (performance, readability, safety)? Or are those equal?
Sometimes there is need to change an integer to text. I often use the following way:
But there is alternative way:
Which one is better (performance, readability, safety)? Or are those equal? |
|||||
|
I would recommend
This allows you to to change myNumber to another primitive type - or a Number - later.
should be avoided since :
|
|||
|
Integer.toString(myNumber) is the safer way because it throws a malformed exception. Do not worry about performance at this level. There is an expression: Premature Optimization. EDIT after comment "3000000000" You can also manage radix :
Output :
EDIT after comment 2 Look in Effective Java and search for StringBuilder() to understant how to replace ' At execution time, a '
This code is better :
Look at Java code of |
|||||||||||||||||||||
|
I would say they were equally as good. I prefer to use
as I find it explains the transition better |
|||||
|