Convert char to int java – Java Program to Convert char to int

Convert char to int java: In the previous article we have discussed Java Program to Convert double to int

In this article we will discuss about how to convert char type to int type.

Program to Convert char to int

Convert char to int in java: Let’s see some example of both the types.

Example-1: char type

char a = '3';
char b= 'Z'
Example-2: int type

int a = 46;
int b = 122;

Now let’s see the different methods to do it.

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Method 1 : Java Program to Convert char to int by Getting ASCII Value:

Convert a char to an int java: We can convert to the character to integer by getting it’s ASCII Value .

When we will assign a char variable to an int variable we will get it’s ASCII value in int.

Approach :

  1. Take a char value and store it in a char  variable input1.
  2. Assign the char variable to an int variable.
  3. Display the result .

Program:

import java.util.Scanner; 
public class Main 
{
    public static void main(String[] args)
    {
  
        // Initializing a character 
        char ch = 'a';
        System.out.println("char value: " + ch);
  
        // Converting ch to it's ASCII int value
        int a = ch;
        System.out.println("int value: " + a);
    }
}
Output :

Enter a Character : a
Converted integer is : 97

Method 2 : Java Program to Convert char to int Using String.valueOf() method:

Java convert char to integer: We can convert to the character to integer by using String.valueOf()

This method of class String can be able to convert various types of values to String value. It can be used to convert int, char, long, Boolean, float, double, object & char array to String. Which will  further can be converted to an int  by using Integer.parseInt() method.

Approach:

  1. Take a char value  and store it in a char variable input1.
  2. Then pass that input1 variable as parameter to Integer.parseInt(String.valueOf ( )) method which will convert the char to int value and return it .
  3. Store that int value in a variable output.
  4. Display the result .

Program:

import java.util.*; 

public class Main
{
    public static void main(String[] args)
    {
    
        char input1='6';
        //converting to it's equivalent int
        int output = Integer.parseInt(String.valueOf(input1));
        System.out.println("Converted integer is :"+output);
    }
}
Output :

Converted integer is : 6

Method 3 : Java Program to Convert char to int Using Character.getNumericValue( ) method:

We can convert to the character to integer by using Character.getNumericValue( ) method.

This method of class character is used to get the int  value of any specific character.

Approach :

  1. Take a char value and store it in a char variable input1.
  2. Then pass that input1 variable as parameter to Character.getNumericValue( ) method which will convert the char to int value and return it .
  3. Store that int value in a variable output .
  4. Display the result .

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        // creating scanner object
        Scanner sc = new Scanner(System.in);
        // input a character through scanner class
        System.out.print("Enter a Character : ");
        char input1=sc.next().charAt(0);
        // converting to it's equivalent int
        int output =Character.getNumericValue(input1);
        System.out.println("Converted integer is :"+output);
    }

}
Output :

Enter a Character : 6
Converted integer is :6

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Related Java Program: