There are two ways to achieve our goal:
1. Using Character.valueOf() method
2. Using new keyword
Read Also: How to Compare Characters in Java
1. Using Character.valueOf() method
You can use Character's class valueOf() method to convert char to Character as shown below: public class CharToCharacter {
public static void main(String args[]) {
// Initializing char
char ch= 'a';
// Converting char to Character
Character ch2 = Character.valueOf(ch);
// Printing Character
System.out.println("Character is: " + ch2);
}
}
Output:
Character is: a
2. Using new keyword
You can use new keyword to convert char to Character in Java as shown below: public class CharToCharacter2 {
public static void main(String args[]) {
// Initializing char
char ch= 'b';
// Converting char to Character using new keyword
Character ch2 = new Character(ch);
// Printing Character
System.out.println("Character is: " + ch2);
}
}
Output:
Character is: b
That's all for today. Please mention in the comments in case you have any questions related to how to convert char to Character in Java.