How to convert char to String in Java? Simple and correct ways
Hello everyone! I just started learning Java and encountered a problem. I need to convert a single character (char) to a string (String). For example, I have a variable char c = 'A', and I want to get the string "A" from it, so I can use it in concatenation or pass it to a method that accepts String.
I tried doing this:
char letter = 'Z';
String result = (String) letter; // Compilation error
But the compiler gives an error: "Cannot cast from char to String". I understand that char and String are different types, but how do I convert it correctly? I've seen many different options on the internet: some use Character.toString(), some write String.valueOf(), and some just add an empty string: "" + letter. Which method is the most correct and why? Is there a difference in perfor