public class Main {
public static void main(String[] args) {
char ch = 'A'; // character
String str = "Hello"; // string
// convert char to string
String charToString = Character.toString(ch);
System.out.println("Character to String: " + charToString);
// convert string to char
char stringToChar = str.charAt(1);
System.out.println("String to Character: " + stringToChar);
}
}
Character to String: A
String to Character: e
ch
and a string str
. To convert a character to a string, we use the Character.toString()
method, which takes a character as a parameter and returns a string
. To convert a string
to a character, we use the charAt()
method of the string
class, which returns the character at the specified index.Character.toString()
and String.charAt()
methods are part of the Java Standard Library, so no additional import statements are required.