Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Convert Char to String in Java?

  • Last Updated : 19 Oct, 2021

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:

Illustration:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input  : 'G'
Output : "G"

Methods:

There are various methods by which we can convert the required character to string with the usage of wrapper classes and methods been provided in java classes.



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

Let us discuss these methods in detail below as follows with clean java programs as follows:

Method 1: Using concatenation of strings 

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

Example

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

Method 2: Using toString() method of Character class 

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

Example



Java




// Java Program to Convert Chhar 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

Method 3: Using Character wrapper class

We can convert a char to  a string  object in java  by using java.lang.Character class, which is a wrapper for 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.

Example

Java




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

Method 4-A: Using String.valueOf() method of String class

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

Example

Java




// Java Program to Convert Chhar 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

Method 4-B: Using valueOf() method of String class 

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

Example

Java




// Java Program to Convert Chhar 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



My Personal Notes arrow_drop_up
Recommended Articles
Page :