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 four listed methods in Java
Example of Conversion of Char to String Java
Input : 'G' Output : "G"
Methods to Convert Char to String
There are various methods by which we can convert the required character to string with the usage of wrapper classes and methods provided in Java classes.
- Using concatenation of strings
- Using toString() method of Character class
- Using Character Wrapper class
- Using valueOf() method of the String class
Let us discuss these methods in detail below as follows with clean Java programs as follows:
1. Using Concatenation of StringsĀ for Char to String Conversion
We can convert a char to a string object in Java by concatenating the given character with an empty String.
Below is the implementation of the above method:
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); Ā Ā Ā Ā } } |
Char to String using Concatenation : G
2. Using the toString() method of the Character classĀ
We can convert a char to a string object in Java by using the Ā Character.toString() method.
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); Ā Ā Ā Ā } } |
Char to String using Character.toString method : G
3. 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); Ā Ā Ā Ā } } |
Char to String using Character.toString method : G
Output:
3-A. 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 Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā */ Ā Ā Ā Ā } } |
Char to String using @Deprecated Annotation and toString method : G
4-A. 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); Ā Ā Ā Ā } } |
Char to String using String.valueOf(new char[]) method : G
4-B. Using valueOf() method of String classĀ
We can convert a char to a string object in Java by using String.valueOf() method.
Below is the 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); Ā Ā Ā Ā } } |
Char to String using String.valueOf() method : G