Convert char to int in Java

In Java, you can convert a char to an int using the (int) casting operator. When you cast a char to an int, the ASCII value of the char is returned as an integer.

Here’s an example program that demonstrates how to convert a char to an int:

public class CharToIntExample {
public static void main(String[] args) {
char c = 'A';
int asciiValue = (int) c;
System.out.println("The ASCII value of " + c + " is " + asciiValue);
}
}


In this program, we declare a char variable c and initialize it to the character ‘A’. We then use the (int) casting operator to convert the char c to an int, and store the result in the variable asciiValue. Finally, we print out the ASCII value of the character using System.out.println(). The output of this program will be:

The ASCII value of A is 65


Note that this conversion works only if the char represents a valid ASCII character. If you want to convert a char to its corresponding Unicode code point, you can use the Character.codePointAt() method.