Java Convert char to intConverting a char to an int in Java is a fundamental operation when dealing with character data. In Java, a char is a 16-bit unsigned integer representing a single Unicode character, while an int is a 32-bit signed integer capable of holding a wider range of values. The conversion process involves obtaining the Unicode value of the char and then converting it to an int. When we convert a char to an int using the (int) casting operation, Java converts the char value to its corresponding Unicode value as an int. For example, if we have a char variable named myChar containing the character 'A', (int) myChar would result in the integer value 65, which is the Unicode value of 'A'. ![]() It's important to note that Unicode is a character encoding standard that assigns a unique number to each character in most languages and scripts used in modern computing. This means that each char in Java corresponds to a specific Unicode value, allowing for consistent representation of characters across different systems and languages. There are the following ways to convert character into integer value.
We can convert char to int in Java using various ways. 1) By Assigning Character in Integer VariableLet's see the simple code to convert char to int in Java. CharToIntExample1.java Test it NowOutput: 97 49 2) Using Character.getNumericValue() MethodLet's see the simple code to convert char to int in Java using Character.getNumericValue(char) method that returns an integer value. CharToIntExample2.java Test it NowOutput: 1 3) Using String.valueOf() MethodLet's see another example which returns integer value of specified char value using String.valueOf(char) method. Test it NowOutput: 1 Let's understand about the conversion of char to int in Java with the help of a Java example program. Example 1:Filename: CharToIntConversion.java Output: Enter a single character: a Method 1: Using (int) casting Character 'a' as int: 97 Method 2: Using Character.getNumericValue(char) Numeric value of 'a': 10 Method 3: Using Character.digit(char, int) Numeric value of 'a' with radix 10: -1 Method 4: Using Unicode value Unicode value of 'a': 97 Additionally, Java provides the Character.getNumericValue(char) method that can be used to convert a numeric char representing a digit (such as '0' to '9') to its corresponding int value. For example, Character.getNumericValue('7') would return the int value 7. In summary, converting a char to an int in Java is a straightforward process that involves converting the Unicode value of the char to an int. This conversion is essential when working with character data and allows for the manipulation and processing of characters in a Unicode-compatible manner. Next TopicJava int to char |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India