8
\$\begingroup\$

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?

asked Jul 18, 2012 at 9:45
\$\endgroup\$
1
  • 4
    \$\begingroup\$ A very detailed answer to a very similar question on SO: stackoverflow.com/a/4105406/843804 \$\endgroup\$ Commented Jul 18, 2012 at 17:11

3 Answers 3

17
\$\begingroup\$

I would recommend

String.valueOf(myNumber)

This allows you to to change myNumber to another primitive type - or a Number - later.

"" + myNumber

should be avoided since :

  • it produces clutter bytecode (instanciating a new StringBuffer)
  • it does not convey what you wanted to do with myNumber
answered Jul 19, 2012 at 1:13
\$\endgroup\$
11
\$\begingroup\$

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" > Integer.MAX_VALUE throw an Exception
"-2900000000" < Integer.MIN_VALUE throw an Exception
"OO12345" throw an exception, because it begins with two O letters, not zero.

You can also manage radix :

String intS = "010101";
System.out.format("Parsing %s gives %d, fail with \"010102\"%n",
 intS,(Integer.parseInt(intS, 2)));
intS = "10CAFE8";
System.out.format("Parsing %s gives %d, fail with \"10CAGE8\"%n",
 intS,(Integer.parseInt(intS, 16)));

Output :

Parsing 010101 gives 21, fail with "010102"
Parsing 10CAFE8 gives 17608680, fail with "10CAGE8"

EDIT after comment 2

Look in Effective Java and search for StringBuilder() to understant how to replace '+' usage.

At execution time, a '+' create something like this code :

StringBuilder sb = new StringBuilder();
sb.append(123);
sb.toString();

This code is better :

final int a = 123;
Integer.toString(a);

Look at Java code of Integer.toString(a);
In Eclipse : CTRL+rightClick on toString() print the Java code . Give the src.zip path in SDK directory if it not already done.
And compare with StringBuilder.append(int);

answered Jul 18, 2012 at 9:52
\$\endgroup\$
5
  • 5
    \$\begingroup\$ How can an integer be malformed? \$\endgroup\$ Commented Jul 18, 2012 at 16:35
  • \$\begingroup\$ @FelixDombek I've edited my post to give samples. \$\endgroup\$ Commented Jul 19, 2012 at 9:57
  • 4
    \$\begingroup\$ Your examples are about String to Int, but the question is about Int to String. \$\endgroup\$ Commented Jul 19, 2012 at 13:05
  • \$\begingroup\$ @FelixDombek Exact, I compelte my post. Sorry not to be a good english reader. \$\endgroup\$ Commented Jul 20, 2012 at 6:07
  • \$\begingroup\$ @FelixDombek Effective Java is a reference to write good code, Is the response you needed? Many other things to know are also inside this book. \$\endgroup\$ Commented Jul 21, 2012 at 15:50
1
\$\begingroup\$

I would say they were equally as good. I prefer to use

Integer.toString(myNumber)

as I find it explains the transition better

palacsint
30.3k9 gold badges81 silver badges157 bronze badges
answered Jul 18, 2012 at 14:17
\$\endgroup\$
1
  • \$\begingroup\$ haha my mistake @palacsint \$\endgroup\$ Commented Jul 20, 2012 at 8:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.