English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)/O)

Java Reader/Writer

Other Java topics

Java program converts char type variable to int

   Java Examples Comprehensive

In this program, we will learn how to convert a character (char) type variable to an integer (int) in Java.

To understand this example, you should understand the followingJava programmingTopic:

Example1: Java program converts char to int

class Main {
  public static void main(String[] args) {
    //Create character variables
    char a = '5;
    char b = 'c';
    //Convert char variable to int
    //Assign char variable to int type variable
    int num1 = a;
    int num2 = b;
    // print the values
    System.out.println(num1);    // 53
    System.out.println(num2);    // 99
  }
}

In the above example, we have char type variables a and b. Note that we have assigned the char variable to an int variable.

In this case, instead of the character, the ASCII value of the character is assigned to an int variable. Therefore, we get the value 53(ASCII value “ 5)And 99(ASCII value “ c”)As output.

Example2: Use getNumericValue() method to convert char to int

We can also use the getNumericValue() method of the Character class to convert char type variables to int type.

class Main {
  public static void main(String[] args) {
    //Create character variables
    char a = '5;
    char b = '9;
    //Convert char variable to int
    // Use the getNumericValue() method
    int num1 = Character.getNumericValue(a);
    int num2 = Character.getNumericValue(b);
    //Print the numeric value of the character
    System.out.println(num1);    // 5
    System.out.println(num2);    // 9
  }
}

Here, as we can see, the getNumericValue() method returns the numeric value of the character. The character ' '5' will be converted to an integer 5and the character ' '9' will be converted to an integer 9.

Example3: Use parseInt() method to convert char to int

We can also use the parseInt() method of the Integer class to convert char type variables to int type.

class Main {
  public static void main(String[] args) {
    // Create character variables
    char a = '5;
    char b = '9;
    // Convert char variable to int
    // Use the parseInt() method
    int num1 = Integer.parseInt(String.valueOf(a));
    int num2 = Integer.parseInt(String.valueOf(b));
    // Print the number
    System.out.println(num1);    // 5
    System.out.println(num2);    // 9
  }
}

Note the expression

Integer.parseInt(String.valueOf(a))

Here,

  • String.valueOf(a) - Convert char type variable a to String type

  • Integer.parseInt()  - Convert string to int

NoteThe Integer.parseInt() method is only applicable to string variables. Therefore, the character 'a' can be converted to a String (string).

Example4: Convert char to int by subtracting character '0'

In Java, we can also convertSubtract 0 from char characterCharacter to convert it to an integer. For example,

class Main {
  public static void main(String[] args) {
    //Create character variables
    char a = '9;
    char b = '3;
    //Convert char variable to int
    //Subtract character 0 from char character
    int num1 = a - "0";
    int num2 = b - "0";
    // print numeric value
    System.out.println(num1);    // 9
    System.out.println(num2);    // 3
  }
}

In the above example, please note the following lines:

int num1 = a -"0";

In this case, the character will be converted to an integer. By subtracting zero from the value, the same value will be obtained. That is9 - 0 = 9.

Therefore, we separately obtain the character " 9" and " 3The integer value of "9And3.

Java Examples Comprehensive