Algorithm


  1. Declare Variables:

    • Declare a char variable to store the character you want to convert.
    • Declare an int variable to store the result of the conversion.
  2. Input:

    • Accept user input or assign a value to the char variable.
  3. Conversion:

    • Use the (int) casting operator to convert the char to an int.
      java
      int intValue = (int) charValue
  4. Display Result:

    • Print or display the original char value and the converted int value.

 

Code Examples

#1 Code Example- Java Program to Convert char to int

Code - Java Programming

class Main {
  public static void main(String[] args) {

    // create char variables
    char a = '5';
    char b = 'c';

    // convert char variables to int
    // ASCII value of characters is assigned
    int num1 = a;
    int num2 = b;

    // print the values
    System.out.println(num1);    // 53
    System.out.println(num2);    // 99
  }
}
Copy The Code & Try With Live Editor

#2 Code Example- char to int using getNumericValue() method

Code - Java Programming

class Main {
  public static void main(String[] args) {

    // create char variables
    char a = '5';
    char b = '9';

    // convert char variables to int
    // Use getNumericValue()
    int num1 = Character.getNumericValue(a);
    int num2 = Character.getNumericValue(b);

    // print the numeric value of characters
    System.out.println(num1);    // 5
    System.out.println(num2);    // 9
  }
}
Copy The Code & Try With Live Editor

#3 Code Example- char to int using parseInt() method

Code - Java Programming

class Main {
  public static void main(String[] args) {

    // create char variables
    char a = '5';
    char b = '9';

    // convert char variables to int
    // Use parseInt()
    int num1 = Integer.parseInt(String.valueOf(a));
    int num2 = Integer.parseInt(String.valueOf(b));

    // print numeric value
    System.out.println(num1);    // 5
    System.out.println(num2);    // 9
  }
}
Copy The Code & Try With Live Editor

#4 Code Example- char to int by subtracting with '0'

Code - Java Programming

class Main {
  public static void main(String[] args) {

    // create char variables
    char a = '9';
    char b = '3';

    // convert char variables to int
    // by subtracting with char 0
    int num1 = a - '0';
    int num2 = b - '0';

    // print numeric value
    System.out.println(num1);    // 9
    System.out.println(num2);    // 3
  }
}
Copy The Code & Try With Live Editor

#5 Code Example with Java Programming

Code - Java Programming

start coding...
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Java Programing Example to convert char type variables to int-DevsEnv