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?