char to Character Java

In this post, I will be sharing how to convert char to Character in Java. The main difference between char and Character is that char is a primitive data type whereas Character is a wrapper class that wraps a value of the primitive type char in an object.

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.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry