Java program to convert char type variables to int
Here's an example Java program to convert a char type variable to an int:
referi:ot giftidea.compublic class CharToIntConversion { public static void main(String[] args) { char c = 'a'; int i = (int) c; System.out.println(i); // Output: 97 } }
In Java, a char type variable is represented as a 16-bit unsigned integer that can hold values between 0 and 65,535. However, char values are commonly used to represent characters from the Unicode character set, with each character being assigned a unique integer value.
To convert a char type variable to an int, you can simply cast the char to an int using the (int) cast operator. In the example above, the character 'a' is cast to an int, resulting in the value 97, which is the Unicode integer value for the lowercase letter 'a'.
Note that because char is an unsigned data type, it can be implicitly converted to an int without a cast. However, it's generally good practice to use an explicit cast to make your code more clear and to avoid potential issues with type promotion.