In Java, converting a char to a String can be done in several ways. Here are some of the common methods:
1. Character.toString(char c)
Character.toString(c) is a static method provided by the Character class. It is explicitly designed to convert a char to a String.
char c = 'B';
String str = Character.toString(c);
System.out.println(str); // Output: "B"
Explanation:
Step 1: We declare a char variable c and assign it the value ‘B’.
Step 2: We use Character.toString(c) to convert the char into a String.
- The Character class provides a static method toString(char c), which takes a char value and returns a String representation of it.
Step 3: We print the String to the console. The result will be “B”.
This method is clear, concise, and specifically designed to handle conversion from char to String.
2. String.valueOf(char c)
String.valueOf(c) is a static method in the String class that converts a variety of primitive types (such as int, boolean, char, etc.) into their corresponding String representations.
char c = 'B';
String str = String.valueOf(c);
System.out.println(str); // Output: "B"
Explanation:
Step 1: Declare a char variable c and assign it the value ‘B’.
Step 2: Use String.valueOf(c) to convert the char into a String.
- String.valueOf() is a static method that can convert different types of data (like char, int, boolean, etc.) into a String. It works by internally calling String.valueOf(char c) for a char type.
Step 3: We print the result (str). The output will be “B”.
This method is very commonly used and is considered a “universal” approach because it works for any type of primitive data type (not just char).
3. String Concatenation (c + “”)
String concatenation is a way to convert a char to a String by concatenating the char with an empty string (“”).
char c = 'B';
String str = c + "";
System.out.println(str); // Output: "B"
Explanation:
Step 1: We declare a char variable c and assign it the value ‘B’.
Step 2: The expression c + “” concatenates the char with an empty string (“”). In Java, when you add a char to a String, the char is automatically converted to a String.
- This works because of how Java handles the + operator. When you concatenate a char with a String, the char is promoted to a String and the two are concatenated.
Step 3: We print the result (str). The output will be “B”.
4. new Character(c).toString()
This method involves creating a Character object, which is a wrapper class for the primitive char. Then, the toString() method of the Character class is invoked, which returns the string representation of the character.
char c = 'B';
String str = new Character(c).toString();
System.out.println(str); // Output: "B"
Explanation:
Step 1: We declare a char variable c and assign it the value ‘B’.
Step 2: We wrap the char in a Character object using new Character(c). The Character class is a wrapper class for the char primitive type.
- This is done because Character is an object, and calling .toString() on any object will return its string representation.
- The Character class has a toString() method that returns the string equivalent of the char.
Step 3: We print the String representation of the Character. The output will be “B”.