What is the best way to convert a primitive data type to String

I can convert an integer to a string using

String s = "" + 4; // correct, but poor style or String u = Integer.toString(4); // this is good 

I can convert double to string using

 String s = "" + 4.5; // correct, but poor style or String u = Double.toString(4.5); // this is good 

I can use the String s = "" + data approach to convert int or double to String. Although if I want to use a different approach using toString() , I have to use the Wrapper class for each data type. Then why in some books it is mentioned that the first approach is bad, and the second is the best. Which one is the best approach and why?

+46
java
Jul 13 '11 at 12:39
source share
9 answers

I would use

 String.valueOf(...) 

You can use the same code for all types, but without the hideous and senseless concatenation of strings.

Note that it also says exactly what you want - a string value corresponding to this primitive value. Compare this to the "" + x approach, where you apply string concatenation even if you have no intention of concatenating anything and an empty string is not suitable for you. (This is probably more expensive, but I see that I am reading more than performance.)

+67
Jul 13 '11 at 12:41
source share

What about String.valueOf() ? It is overridden overloaded for all primitive types and delegates toString() for reference types.

+32
Jul 13 '11 at 12:41
source share
 String s = "" + 4; 

Compiled using this code:

 StringBuffer _$_helper = new StringBuffer(""); _$_helper.append(Integer.toString(4)); String s = _$_helper.toString(); 

Obviously, this is pretty useless. Keep in mind that behind the scenes, the compiler always uses StringBuffers if you use + in asociation with a string

+10
Jul 13 2018-11-11T00:
source share

There the third is String.valueOf(..) (which calls Wrapper.toString(..)

In fact, the compiler adds Wrapper.toString(..) in these places, so it is the same in terms of bytecode. But ""+x is more ugly.

+9
Jul 13. '11 at 12:41
source share

The string concatenation method creates an additional object (which then receives the GCed), which is one of the reasons why it is considered "poorer". Plus it’s more complicated and less readable, which, as John Skeet points out, is usually a big consideration.

+6
Jul 13 '11 at 12:43
source share

I would also use the String.valueOf() method, which essentially uses a Wrapper object of a primitive type and calls the toString() method for you:

Example: in the String class:

 public static String valueOf(int i) { return Integer.toString(i, 10); } 
+4
Jul 13 '11 at 12:43
source share

Adding a double quote is a bad way to make it especially for readability. I would consider using some Apache classes to convert or write my own usage methods for this type of thing.

+2
Jul 13 '11 at 12:41
source share

It is always better that you know the type of argument you are trying to convert to a string, and the compiler also knows about the type. This simplifies work as well as loops. When you follow the append method, you leave the type decision to the compiler, and also increase the lines of code for the compiler to do the same.

+1
Jul 13 '11 at 13:52
source share

I think the answer really depends on what you are trying to convert, and for what purpose, but in general I am not a big fan of doing naked conversions, because in most cases conversion to a string for logging or other human readability purposes.

 MessageFormat.format("The value of XYZ object is {0}", object); 

This gives good readability, fine-grained control over the formatting of the output, and, importantly, it can be internationalized by replacing the line with a link to the message.

Should I mention this, also avoids the possible NPE problem when calling object.toString ()?

+1
Jul 14 2018-11-11T00:
source share



All Articles