I see four alternatives for converting a char
to a Stirng
in Java.
v = Something.lookup(new String((char)binaryData[idx])); // SORRY! Wrong.
v = Something.lookup("" + (char)binaryData[idx]);
v = Something.lookup(String.valueOf((char)binaryData[idx]));
v = Something.lookup(Character.toString((char)binaryData[idx])));
I think that the first is the slowest. The second is very convenient. I speculate that the third may return a previously created String
instance, but I'm not sure, and the API documentation does not say so. The same applies to option four. This reusing of instance would be very fortunate because then a hash based lookup could take advantage of hashCode()
caching in String
. (Which feature is also not described in the API documentation, but many people told me.)
I'm coming from C++ and I feel this lack of complexity informations disturbing. :-) Are my speculations correct? Do we have any kind of official documentation where performance guaranties and the caching mechanisms declared?
In fact, String is made of Character array in Java. Char is 16 bit or 2 bytes unsigned data type. //toString method take character parameter and convert string. //valueOf method take character parameter and convert string. We can convert a String to char using charAt () method of String class.
Using valueOf () Method The valueOf () method is a static method of the String class that is also used to convert char [] array to string. The method parses a char [] array as a parameter. It returns a newly allocated string that represents the same sequence of characters contained in the character array.
Char is 16 bit or 2 bytes unsigned data type. public class CharToString_toString { public static void main (String [] args) { //input character variable char myChar = 'g'; //Using toString () method //toString method take character parameter and convert string.
Converting String to int or Integer If we need to convert a String to primitive int or Integer wrapper type, we can use either the parseInt () or valueOf () APIs to get the corresponding int or Integer return value: 3. Converting String to long or Long
First of all, the Java specification does not say anything about performance concerning these four methods, hence the results may vary depending on the JRE version and vendor you use.
If you use Oracle's JRE, you can easily inspect the source code by yourself! In Java 8, it is as follows:
Given a char c
with some value:
new String(c)
doesn't compile. No such constructor."" + c
looks ugly, cumbersome and tricky. Internally it creates a new empty StringBuilder
and appends the character to it. Then it creates a new String
instance out of the StringBuilder
.Character.toString(c)
delegates to String.valueOf(c)
.String.valueOf(c)
creates a new String
instance.So which one to use?
The most readable!
That's String.valueOf(c)
or Character.toString(c)
in my point of view!
The second one is certainly(in theory) slower, as it is translated into
v = Something.lookup(new StringBuilder().append("").append((char)binaryData[idx]).toString());
StringBuilder
s are implemented using a char[]
initialized to hold 16 values. The StringBuilder
option therefore initializes a char[]
of size 16, only to copy the cells that are set (only the first one in this case) to the resulting string.
String.valueOf
(which is equivalent to Character.toString
) uses a char[]
of size 1, and then directly sets the String
's backing char[]
, thereby avoiding the need for a copy.
The first approach will not compile (at least not under java 7), as there is no String
constructor accepting a single character as input: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With