Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Nice question. I've got of the following <strike>five</strike> 6 methods to do it.</p> <pre><code>1. String stringValueOf = String.valueOf('c'); // most efficient 2. String stringValueOfCharArray = String.valueOf(new char[]{x}); 3. String characterToString = Character.toString('c'); 4. String characterObjectToString = new Character('c').toString(); // Although this method seems very simple, // this is less efficient because the concatenation // expands to new StringBuilder().append(x).append("").toString(); 5. String concatBlankString = 'c' + ""; 6. String fromCharArray = new String(new char[]{x}); </code></pre> <blockquote> <p>Note: <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Character.java#Character.toString%28char%29" rel="noreferrer">Character.toString(char)</a> returns <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.valueOf%28char%29" rel="noreferrer">String.valueOf(char)</a>. So effectively both are same.</p> </blockquote> <p><code>String.valueOf(char[] value)</code> invokes <code>new String(char[] value)</code>, which in turn sets the <code>value</code> char array.</p> <pre><code>public String(char value[]) { this.value = Arrays.copyOf(value, value.length); } </code></pre> <p>On the other hand <code>String.valueOf(char value)</code> invokes the following <strong>package private constructor.</strong></p> <pre><code>String(char[] value, boolean share) { // assert share : "unshared not supported"; this.value = value; } </code></pre> <p><em>Source code from <code>String.java</code> in <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/String.java" rel="noreferrer">Java 8 source code</a></em></p> <blockquote> <p>Hence <code>String.valueOf(char)</code> seems to be most efficient method, in terms of both memory and speed, for converting <code>char</code> to <code>String</code>.</p> </blockquote> <ol> <li><a href="https://deepumohan.com/tech/java-how-to-convert-primitive-char-to/" rel="noreferrer">How to convert primitive char to String in Java</a></li> <li><a href="http://javarevisited.blogspot.in/2012/02/how-to-convert-char-to-string-in-java.html" rel="noreferrer">How to convert Char to String in Java with Example</a></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload