27

I am using the following approach to convert any primitive data type to string

Example

int i = 5;//
String convertToString = ""+i;// convert any primitive data type to string

To convert int data type to string i can use Integer.toString() but what is the better way to convert any type of primitive data (not only int) types to string

4
  • 1
    Doesn't everything in Java have .toString() method? Commented Dec 26, 2011 at 11:48
  • 1
    @SergeiTulentsev primitives don't Commented Dec 26, 2011 at 11:51
  • 3
    primitive types are not objects so they don't have toString() Commented Dec 26, 2011 at 11:51
  • 1
    jeezz.. primitives surely don't have any methods, I was just answering @SergeiTulentsev question, because in the above example toString() can't be applied to primitive type int Commented Dec 26, 2011 at 11:54

4 Answers 4

43

Use String.valueOf() method.

int no = 2;

String strValue = String.valueOf( no );
Sign up to request clarification or add additional context in comments.

11 Comments

@HarryJoy Which one? Do you mean string concatenation or toString()
The main adavantage is that there is a String.valueOf method for boolean, char, int, long, float, double and Object, so the same method name can be used to convert anything to a String.
@JBNizet : I'm not against valueOf() method. Actually I'm curious about why this is the best/better to use among all conversion methods?
It's better, because it creates one new String instance instead three. ""+i is probably translated into "".concat(String.valueOf(1));
Advantage is: it creates string in the reusable string literal pool.
|
5

I recently ran some benchmarks to compare ""+myInt vs Integer.toString(myInt).

And the winner is... Integer.toString() !

Because it does not create temporary strings, uses only a adequately-sized char buffer, and some funky algorithms to convert from a digit to its char counterpart.

Here is my blog entry if you read french (or use the sidebar translation widget if you don't)

Comments

3

Consider this example :

int i = 42; 
\\ String 
str = Integer.toString(i);

In this case you could also do :

String str = "" + i; 

Similarly, one of the easiest way to convert primitive datatypes to String is to use the toString() method with the datatype object of the element to be converted.

 String str = Double.toString(d); // Convert double to String
 String str = Long.toString(l);  //Convert long to String
 String str = Float.toString(f); //Convert float to String

Comments

1

You need to define "better". The most efficient way for the machine is to use

String.valueOf(no);

or

Integer.toString(no);

however the most efficient use of your time is to make the code as simple as possible.

"" + no;

This is fairly hot contested decision which reminds me of this quote

DEVENTER (n) A decision that's very hard to make because so little depends on it, such as which way to walk around a park

-- The Deeper Meaning of Liff by Douglas Adams and John Lloyd.

11 Comments

Will you please care to explain "Why String.valueOf(no); is the most efficient way for the machine?"
+1 Even though it's "hackalicious", I favour no + "" for its brevity and clarity
I don't find it clear. String.valueOf() and Integer.toString() express the intention clearly. no + "" doesn't, IMHO.
"a" + no. But in this case, the intent is to concatenate. Not to convert.
With PMD becoming a standard tool in Java development, this idiom is less and less frequent, because PMD has a rule to avoid it: pmd.sourceforge.net/rules/optimizations.html#AddEmptyString
|

Your Answer

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.