Saturday, September 21, 2024
Google search engine
HomeData Modelling & AIHow to Convert Char to String in Java?

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 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.

  1. Using concatenation of strings
  2. Using toString() method of Character class
  3. Using Character Wrapper class
  4. 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);
Ā Ā Ā Ā }
}


Output

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);
Ā Ā Ā Ā }
}


Output

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);
Ā Ā Ā Ā }
}


Output

Char to String using Character.toString method : G

Output:

Output for Program to Convert Char to String Java

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
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā */
Ā Ā Ā Ā }
}


Output

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);
Ā Ā Ā Ā }
}


Output

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);
Ā Ā Ā Ā }
}


Output

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

Whether youā€™re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions weā€™ve already empowered, and weā€™re here to do the same for you. Donā€™t miss out ā€“ check it out now!

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?