How to Convert Char to String in Java?
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.
- Using toString() method of Character class
- Using valueOf() method of the String class
- 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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
char c = 'G' ;
String s = Character.toString(c);
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
char c = 'G' ;
String s = String.valueOf(c);
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
char c = 'G' ;
String s = "" + c;
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
char c = 'G' ;
String s = Character.toString(c);
System.out.println(
"Char to String using Character.toString method :"
+ " " + s);
}
}
|
Output
Char to String using Character.toString method : G
Output:
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
import java.io.*;
import java.util.*;
class GFG {
@Deprecated
public static void main(String[] args)
{
Character ch = new Character( 'G' );
System.out.println(
"Char to String using @Deprecated Annotation and toString method :"
+ " " + ch.toString());
}
}
|
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
char c = 'G' ;
String s = String.valueOf( new char [] { c });
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
Share your thoughts in the comments
Please Login to comment...