What is the most direct and/or efficient way to convert a char[]
into a CharSequence
?

- 55,321
- 43
- 129
- 155
-
Just curious! Why do you need the CharSequence? – bruno conde Nov 18 '08 at 18:29
-
Because I'm writing to an Appendable. – Chris Conway Nov 18 '08 at 18:37
3 Answers
Without the copy:
CharSequence seq = java.nio.CharBuffer.wrap(array);
However, the new String(array)
approach is likely to be easier to write, easier to read and faster.

- 145,806
- 30
- 211
- 305
-
I chose the most direct way. yours is probably the most efficient, you can't have both i guess – jjnguy Nov 18 '08 at 18:14
-
Direct? More direct not to copy, surely? But the CharBuffer subclass code is probably less well exercised, so might end up being slower. – Tom Hawtin - tackline Nov 18 '08 at 18:26
-
I'm not sure why you think CharBuffer.wrap will be slower? Just because the code is less mature? Surely if I'm doing this in a tight loop, I should prefer the copy-free version? – Chris Conway Nov 18 '08 at 18:28
-
It's well known that going through NIO buffers can be a bit slow (there's an awful lot to inline, and that may not happen as you expect). OTOH, CharSequence is often slower the getChars. – Tom Hawtin - tackline Nov 19 '08 at 17:16
-
This should have less overhead than a CharBuffer: https://gist.github.com/ncruces/ca9f91d89630d27ff05e35410a89022b – Nuno Cruces Jul 27 '17 at 15:47
-
1Not making a copy and not using a String is very important when dealing with char[] that happens to contain a password. So your CharBuffer.wrap solution works great in that case. I also don't see any speed issues when looking into the source code down this path either. – aerobiotic Aug 03 '17 at 10:25
-
2@tackline -- This answer is getting on to 10 years old now. I'm wondering if you still think the CharBuffer is likely to be slower than copying the array into a String? – Jules Sep 05 '17 at 00:03
-
A String
is a CharSequence
. So you can just create a new String
given your char[]
.
CharSequence seq = new String(arr);

- 136,852
- 53
- 295
- 323
Context:
One of the most common usage of char[] instead of String, is to "temporary" store secrets/passwords. To pass it to initialization of some service/clients ... The sercrets are not needed after such initialization. But in java string is not possible to clear it from memory (manualy nor by garbage collection)... So, it is basically forbiden to store secrets in Strings.
Recommended way: Load secrets to char[], pass it to init proces, and clear it manually (set forEach char[i] = '0';). Read about this problem on specialized blogs...
Question/Answer:
- if service/cliets API accepts only pass/secret as a string - don't use it (and report bug)
- if service/cliets API accept char array, use it and clear it
- if service/cliets API accept CharSequence, java.nio.CharBuffer.wrap(array) could be used and cleared after
NOTE: unfortunately, one has to check even 3rd party service/client init source code, it happens that they convert char array to string somewhere deep in their code... )-: Choose your dependencies wisely.

- 1,091
- 12
- 23