Open In App

How to Convert Char to String in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to Convert Char to String in Java. If we have a char value like ‘G’ and we want to convert it into an equivalent String like “G” then we can do this by using any of the following three listed methods in Java.

  1. Using toString() method of Character class
  2. Using valueOf() method of the String class
  3. Using concatenation of strings

Example of Conversion of Char to String Java

Input  : 'G'   // Character
Output : "G"  // String

Methods to Convert Char to String

Let’s discuss three methods in detail for Convert Char to String in Java:

1. Using Character.toString() Method

We can convert a char to a string object in Java by using the  Character.toString() method of Character class.

Below is the Java implementation of the above method:

Java




// Java Program to Convert Char to String
// Using toString() method of Character class
  
// Importing the required packages
import java.io.*;
import java.util.*;
  
// Main class
class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring a char variable
        char c = 'G';
  
        // Converting the char to String using toString
        // method of Character class and storing it in a
        // string
        String s = Character.toString(c);
  
        // Print and display the above string
        System.out.println(
            "Char to String using Character.toString():"
            + " " + s);
    }
}


Output

Char to String using Character.toString method : G

2. Using valueOf() method

We can convert a char to a string object in Java by using String.valueOf() method of String Class.

Below is the java implementation of the above method:

Java




// Java Program to Convert Char to String
// Using valueOf() method of String class
  
// Importing the required packages
import java.io.*;
import java.util.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring a char variable
        char c = 'G';
  
        // Converting char to String
        // using String.valueOf() method
        String s = String.valueOf(c);
  
        // Print and display the String s
        System.out.println(
            "Char to String using String.valueOf() method :"
            + " " + s);
    }
}


Output

Char to String using String.valueOf() method : G

3. Java char to String Example: Using Concatenation of Strings

We can convert a char to a string object in Java by concatenating the given character with an empty String.

Below is the java implementation of the above appraoch:

Java




// Java Program to Convert Char to String
// Using Concatenation in Strings
  
// Importing the required packages
import java.io.*;
import java.util.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Declaring a char variable
        char c = 'G';
  
        // Concatenating the char variable
        // with an empty string
        String s = "" + c;
  
        // Print and display the above string
        System.out.println(
            "Char to String using Concatenation :"
            + " " + s);
    }
}


Output

Char to String using Concatenation : G

Java char to String: Other Examples

a) Using Character Wrapper Class for Char to String Conversion

We can convert a char to a string object in Java by using java.lang.Character class, which is a wrapper for the char primitive type.

Note: This method may arise a warning due to the new keyword as Character(char) in Character has been deprecated and marked for removal.

Below is the implementation of the above method:

Java




// Java Program to Convert Char to String
// Using toString() method of Character class
  
// Importing the required packages
import java.io.*;
import java.util.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Declaring a char variable
        char c = 'G';
  
        // Converting the char to String using toString
        // method of Character class and storing it in a
        // string
        String s = Character.toString(c);
  
        // Print and display the above string
        System.out.println(
            "Char to String using Character.toString method :"
            + " " + s);
    }
}


Output

Char to String using Character.toString method : G

Output:

Output for Program to Convert Char to String Java

Command Prompt

b) Using @deprecated annotation with Character.toString().

This will help you to avoid warnings and let you use deprecated methods. This will help you remove the warnings as mentioned in Method 3

Below is the implementation of the above method:

Java




// Importing the required packages
import java.io.*;
import java.util.*;
  
// Main class
class GFG {
  
    @Deprecated
    // Main driver method
    public static void main(String[] args)
    {
  
        Character ch = new Character('G');
  
        // Print and display the above string
        System.out.println(
            "Char to String using @Deprecated Annotation and toString method :"
            + " " + ch.toString());
  
        /*  This method will arise a warning since the new
           keyword in Character(char) in Character has been
           deprecated and marked for removal. That is why we
            have used @Deprecated annotation
          */
    }
}


Output

Char to String using @Deprecated Annotation and toString method : G

c) Using the String.valueOf() method of the String class

We can convert a char to a string object in Java by using String.valueOf(char[]) method.

Below is the implementation of the above method:

Java




// Java Program to Convert Char to String
// Using String.valueOf() method of String class
  
// Importing the required packages
import java.io.*;
import java.util.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Declaring a char variable
        char c = 'G';
  
        // Converting char to String by
        // using String.valueOf() method
        String s = String.valueOf(new char[] { c });
  
        // Print and display the above-stored string
        System.out.println(
            "Char to String using String.valueOf(new char[]) method :"
            + " " + s);
    }
}


Output

Char to String using String.valueOf(new char[]) method : G



Last Updated : 14 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads