Question

Convert a char to a String in Java

javastringchartype-conversion

Question

I have a Java char value and need to convert it into a String. What is the correct way to convert a char to a String?

Short Answer

You will learn the difference between Java's char and String types, how to convert between them safely, and which conversion method to prefer in production code.

Concept

A Java char stores exactly one UTF-16 code unit, written with single quotes:

char grade = 'A';

A String stores text, including zero or more characters, written with double quotes:

String gradeLabel = "A";

Although both can represent a visible letter, they are different types. A char is a primitive value, while a String is an object. You must create a new String representation rather than casting a char directly.

The clearest general-purpose conversion is:

String text = String.valueOf(grade);

This matters whenever an API expects text: building messages, appending data to a String, storing values in text-based collections, or sending data in a response.

Mental Model

Think of a char as one letter tile, such as A. A String is a text label that can hold one or many tiles, such as "A" or "Apple".

Converting char to String places the one letter tile into a text label. The visible result may look the same, but Java treats the two containers differently.

Take Quiz

Syntax and Examples

Use String.valueOf(char) when you want a String from one character.

char initial = 'J';
String initialText = String.valueOf(initial);

System.out.println(initialText); // J

String.valueOf(initial) returns a new string containing the character J.

You can also use Character.toString(char):

char symbol = '!';
String symbolText = Character.toString(symbol);

System.out.println(symbolText); // !

Both are correct. String.valueOf is commonly preferred because it also has overloads for values such as int, double, and boolean.

A short concatenation alternative also works:

char letter = 'Z';
String text = "" + letter;

However, String.valueOf(letter) is more explicit about your intent.

Step by Step Execution

Consider this program:

char status = 'Y';
String message = "Answer: " + String.valueOf(status);

System.out.println(message);

Step by step:

  1. status stores the single character 'Y'.
  2. String.valueOf(status) creates the string "Y".
  3. Java concatenates "Answer: " and "Y".
  4. message becomes "Answer: Y".
  5. println displays:
Answer: Y

Real World Use Cases

Common places to convert a char to a String include:

  • Displaying a grade: Convert 'A' into text for a report or UI label.
  • Building log messages: Add a character status code to a readable message.
  • Form validation: Turn a character from user input into text for an error message.
  • Text processing: Convert a character returned by String.charAt() before passing it to a method that accepts String.
  • API data: Include a single-character value in JSON-like text, query parameters, or response messages.
Take Quiz

Real Codebase Usage

In real Java projects, conversion usually happens at a boundary where a character becomes part of larger text.

Build a readable message

char priority = 'H';
String logMessage = "Selected priority: " + priority;

Java automatically converts priority during string concatenation. Use this for simple messages.

Pass a character to a method that requires String

public static boolean isAllowedCode(String code) {
    return code.equals("Y") || code.equals("N");
}

char input = 'Y';
boolean allowed = isAllowedCode(String.valueOf(input));

Validate before continuing with a guard clause

char answer = 'X';
String answerText = String.valueOf(answer).toUpperCase();

if (!answerText.equals("Y") && !answerText.equals("N")) {
    throw new IllegalArgumentException("Answer must be Y or N");
}

For repeated text building inside loops, prefer StringBuilder rather than repeatedly concatenating strings.

Common Mistakes

Casting a char to String

This does not compile:

char letter = 'A';
String text = (String) letter; // Compilation error

A cast only works for compatible types. char is a primitive, not a String. Use String.valueOf(letter) instead.

Using single quotes for a string

This is a char, not a String:

String text = 'A'; // Compilation error

Use double quotes for strings:

String text = "A";

Confusing a character with its numeric value

char letter = 'A';
int number = letter;
System.out.println(number); // 65

Assigning a char to an integer produces its UTF-16 numeric value. To get text, use:

String text = String.valueOf(letter); // "A"

Assuming every visible Unicode symbol fits in one char

Many common characters fit in one char, but some Unicode symbols, including many emoji, require two char values in Java's UTF-16 representation. Use String or Unicode code point APIs when processing arbitrary user text.

Comparisons

ApproachExampleWhen to use
String.valueOfString.valueOf('A')Preferred clear, general-purpose conversion
Character.toStringCharacter.toString('A')Clear when working specifically with char values
Concatenation"" + 'A'Short, simple expressions; less explicit
Direct cast(String) 'A'Never; it does not compile

char versus String:

TypeExampleRepresents
char'A'One UTF-16 code unit
String"A"A sequence of text characters, possibly empty
Take Quiz

Cheat Sheet

char letter = 'A';

// Recommended
String text = String.valueOf(letter);

// Also correct
String text2 = Character.toString(letter);

// Works, but is less explicit
String text3 = "" + letter;

Rules:

  • Use single quotes for char: 'A'.
  • Use double quotes for String: "A".
  • Do not write (String) letter; Java cannot cast a primitive char directly to String.
  • A char contains one UTF-16 code unit; a String can contain many characters or be empty.
  • For arbitrary Unicode symbols, prefer working with String when a single visible symbol might use more than one char.

FAQ

How do I convert a char to a String in Java?

Use String.valueOf(character):

String text = String.valueOf('A');

Can I cast a char to String in Java?

No. (String) someChar does not compile. Use String.valueOf(someChar) or Character.toString(someChar).

Is Character.toString() better than String.valueOf()?

Both correctly convert a char. String.valueOf() is often chosen because it uses the same familiar method for other primitive types too.

Does "" + character convert a char to String?

Yes. String concatenation causes Java to convert the char to text. It works, but String.valueOf(character) is usually easier to read.

What is the difference between 'A' and "A" in Java?

'A' is a char. "A" is a String containing one character.

Can every emoji be stored in a Java char?

No. Some Unicode characters, including many emoji, require two char values in Java. Store and process them as String values when needed.

Take Quiz

Mini Project

Description

Create a small initials formatter for a user profile. It accepts two char values, converts them to strings, normalizes their case, and produces a display label such as A. B..

Goal

Convert character initials into a formatted String suitable for display.

Requirements

Accept two char initials. Convert each initial to a String. Convert the initials to uppercase. Return a formatted result with periods and a space. Demonstrate the formatter with sample input.

Take Quiz

Keep learning

Related questions

View more