开发者

What is the better approach to convert primitive data type into String

I can convert an integer into string using

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

I can convert a double into string using

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

I can use String s = 开发者_如何学运维"" + dataapproach to convert either an int or double into String. While If I wants to use the other approach using toString() I have to use the Wrapper class of each data type. Then why in some books it is mentioned that the first approach is poor one while the second one is the better. Which one is the better approach and why?


I would use

String.valueOf(...)

You can use the same code for all types, but without the hideous and pointless string concatenation.

Note that it also says exactly what you want - the string value corresponding to the given primitive value. Compare that with the "" + x approach, where you're applying string concatenation even though you have no intention of concatenating anything, and the empty string is irrelevant to you. (It's probably more expensive, but it's the readability hit that I mind more than performance.)


How about String.valueOf()? It's overridden overloaded for all primitive types and delegates to toString() for reference types.


String s = "" + 4;

Is compiled to this code:

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

Obviously that is pretty wastefull. Keep in mind that behind the scene the compiler is always using StringBuffers if you use + in asociation with String's


There's a third one - String.valueOf(..) (which calls Wrapper.toString(..)

Actually the compiler adds Wrapper.toString(..) in these places, so it's the same in terms of bytecode. But ""+x is uglier.


The string-concatenation way creates an extra object (that then gets GCed), that is one reason why it's considered "poorer". Plus it is trickier and less readable, which as Jon Skeet points out is usually a bigger consideration.


I would also use String.valueOf() method, which, in effect, uses the primitive type's Wrapper object and calls the toString() method for you:

Example, in the String class:

public static String valueOf(int i) {
        return Integer.toString(i, 10);
    }


Appending a double quote is a bad way of doing it especially for readability. I would consider using some of the Apache util classes for conversion or writing your own utility methods for doing this type of stuff.


It is always better that you're aware of the type of argument you are trying to convert to string and also make compiler aware of the type. That simplifies the operation as well as the cycles. When you follow the append method, you are leaving the type decision to the compiler and also increasing the code lines for the compiler to do the same.


I think the answer really depends on what you're trying to convert, and for what purpose, but in general, I'm not a big fan of doing naked conversions, because in most instances, conversions to a string are 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 string with a message bundle reference.

Need I mention this also avoids the possible NPE problem of calling object.toString()?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜