Explain Codes LogoExplain Codes Logo

How to convert a char to a String?

java
best-practices
performance
functions
Nikita BarsukovbyNikita Barsukov·Sep 8, 2024
TLDR

For a swift and easy char to String conversion in Java, you can append the char to an empty String:

char c = 'A'; String s = "" + c; // "A", but not the Captain America one.

Or for a clearer picture, use the String.valueOf(char) method:

String s = String.valueOf(c); // "A", plain and simple.

Both methods give you a String with 'A'.

Efficient code implementation

For optimal performance, consider using String.valueOf(char):

String s = String.valueOf(c);

This is highly efficient and straightforward. A similar efficient solution is Character.toString(char):

String s = Character.toString(c);

You might avoid "" + c as it implicitly uses StringBuilder and could be a cause of extraneous memory usage.

Dealing with multiple concatenations? You would rather explicitly use StringBuilder:

StringBuilder builder = new StringBuilder(); builder.append(c); String s = builder.toString(); // Much better, isn't it?

This leaves room for subsequent append operations without overusing resources.

Peek under Java's hood

Nothing beats knowing what really happens behind the scenes. String.valueOf(char) directly returns a String that represents the specified char. The magic in Character.toString(char)? It eventually calls String.valueOf(char).

Exploring unconventional paths new String(new char[]{char}) is valid, albeit less conventional:

String fromCharArray = new String(new char[]{char});

Give new Character(char).toString() a pass. It puts unnecessary boxing overhead:

String lessRecommended = new Character(c).toString(); // Overhead alert!

And about the enigmatic "" + c operation:

String s = "" + c;

Java secretly uses a StringBuilder and then calls toString() on it. It isn’t something to lose sleep over for single operations, but in repetition or loops, look for better practices.

Other conversion methods

Apart from the usual methods, there are a few more tricks in the magic hat to convert a char into a String:

  • Say 'Hello!' to String.format:
String s = String.format("%c", c); // I can do it too!
  • Be mesmerized by the power of Character array initialization:
String s = new String(new char[]{c}); // Unconventional? Yes. Invalid? No.

Choose the most readable method that is performance-efficient for your needs.

Choosing the right conversion method

In the race to pick the right conversion method, factor in your context:

  • For random and infrequent conversions, feel free to use any method.
  • When dealing with memory-sensitive environments, avoid the "" + char pattern.
  • In a world full of loops or repeated concatenations, StringBuilder is your go-to hero.

Here's an instance where efficiency matters a lot:

// This is a loop we'd rather not loop into. for(int i = 0; i < largeArray.length; i++) { s += largeArray[i]; } // This is how you loop the loop. StringBuilder builder = new StringBuilder(); for(int i = 0; i < largeArray.length; i++) { builder.append(largeArray[i]); } String s = builder.toString();

Watch out for pitfalls and tips

  • The null characters might give you a shock. Character.toString('\u0000') will not return "null" but a String containing the null character.
  • Be alert about character coding. Characters beyond the Basic Multilingual Plane (BMP) might show up as two char units.
  • A little heads-up here: Java Strings are immutable. Each concatenation could be a new object in disguise.