How to Convert Char to String in Java?
Last Updated :
14 Dec, 2023
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
// 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);
}
}
OutputChar 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);
}
}
OutputChar 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);
}
}
OutputChar 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);
}
}
OutputChar 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
// 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
*/
}
}
OutputChar 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);
}
}
OutputChar to String using String.valueOf(new char[]) method : G
Similar Reads
How to Convert a String to URL in Java?
In Java, a string is a sequence of characters. A URL, which is short for Uniform Resource Locator, refers to a web resource that specifies its location on a computer network and provides the mechanism for retrieving it. To represent a URL in Java, we use the java.net.URL class. In this article, we w
2 min read
How to Convert a String to a Character in Java?
String and char are fundamental and most commonly used datatypes in Java. A String is not a primitive data type like char. To convert a String to char in Java, we have to perform character-based operations or have to process individual characters.In this article, we will learn how to convert a Strin
3 min read
How to Convert a String to a Path in Java?
In Java, we can convert a String representation of a file or directory path into a path object using the Paths utility class. It is a part of the java.nio.file package. The Paths class provides a method called get(String file path location) that converts a sequence of strings representing a path int
2 min read
How to Convert a String to a UUID in Java?
A Universally Unique Identifier (UUID) is a 128-bit label utilised for information in computer systems. The term Globally Unique Identifier (GUID) is also used especially in Microsoft systems. UUIDs are regularised by the Open Software Foundation (OSF) as part of the Distributed Computing Environmen
2 min read
How to Convert a String to an Long in Java ?
In Java, String to Integer Conversion is not always effective as the size of the String can overpass Integer. In this article, we will learn the method provided by the Long class or by using the constructor of the Long class. Example of String to Long ConversionInput: " 9876543210"Output: 9876543210
2 min read
How to Convert a String to ArrayList in Java?
Converting String to ArrayList means each character of the string is added as a separator character element in the ArrayList. Example: Input: 0001 Output: 0 0 0 1 Input: Geeks Output: G e e k s We can easily convert String to ArrayList in Java using the split() method and regular expression. Paramet
1 min read
How to Convert a String to an Numeric in Java?
In Java programming, there are situations in which we must convert a string of numeric characters into one of the following data types: double, float, long, or int. To execute arithmetic operations and other numerical calculations, this conversion is necessary. We will look at how to convert a strin
2 min read
Java Program to Convert Char to Int
Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s
4 min read
Convert String to Stream of Chars in Java
The StringReader class from the java.io package in Java can be used to convert a String to a character stream. When you need to read characters from a string as though it were an input stream, the StringReader class can be helpful in creating a character stream from a string. In this article, we wil
2 min read
Convert a String to Character Array in Java
Converting a string to a character array is a common operation in Java. We convert string to char array in Java mostly for string manipulation, iteration, or other processing of characters. In this article, we will learn various ways to convert a string into a character array in Java with examples.E
2 min read