New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Java Character charValue() Method

Java charValue() method belongs to the Character class of the java.lang package. This method is used to convert a Character object into its primitive char value.

Syntax:

public char charValue()  

Parameters:

No parameters are passed in this method.

Returns:

Returns the char value of the Character Object.

Example 1:

Here, the Character Objects are created and initialized with a char value, and the character value is returned.

import java.lang.Character;

public class StudyTonight 
{  
  public static void main(String[] args)
  {        
       Character ch1 = 'M';  //Creation of Character Object
       System.out.print("The character value is = ");  
       System.out.println(ch1.charValue());  
       
       Character ch2 = 'O';  
       System.out.print("The character value is = ");  
       System.out.println(ch2.charValue());
       
       Character ch3 = '1';  
       System.out.print("The character value is = ");  
       System.out.println(ch3.charValue());
       
    }  
}  


The character value is = M
The character value is = O
The character value is = 1

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent char value.

import java.lang.Character;
import java.util.Scanner;

public class StudyTonight 
{  
  public static void main(String[] args)
  {        
       try
       {
         System.out.println("Enter Character Object value");
         Scanner sc = new Scanner(System.in);
         char c = sc.next().charAt(0);
         Character chr = c;
         System.out.println("The character value is " +chr);
       }
       
       catch(Exception e)
       {
         System.out.println(" Invalid input");
       }
  }
      
}  


Enter Character Object value
88
The character value is 8
***********************************
Enter Character Object value
2
The character value is 2

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.