Java Program to convert char type variables to int
What are type conversion?
Type conversion, also known as type casting, in Java is the process of converting one data type to another data type. There are two types of type conversion in Java: Implicit type conversion and Explicit type conversion.
In this Article, we will write a program to convert char type variables to int.
char to int conversion :
Conversion of a char type variable to an int in Java is the process of converting a single 16-bit Unicode character to a 32-bit signed integer. This can be done in a few ways:
1. Implicit conversion:
A char variable can be assigned to an int variable without an explicit cast. This is known as an implicit conversion.
Example:
import java.util.*; public class Main{ public static void main(String[] args) { // initializing character char c = 'A'; // assigning char variable to int int i = c; System.out.println("The ASCII value of " + c + " is " + i); } }
Output: The ASCII value of A is 65
Explanation:
This program declares a char variable c and assigns it the value of ‘A’. Then it declares an int variable i and assigns it the value of c without any explicit cast. Since a char is smaller than an int, the assignment is allowed.
When you run this program, it will print out “The ASCII value of A is 65”, showing that the char variable c has been implicitly converted to an int and that its value is 65 which is the ASCII value of ‘A’.
2. Explicit conversion :
using a cast: A char variable can be explicitly converted to an int variable by using a cast.
Example:
import java.util.*; public class Main{ public static void main(String[] args) { // Initializing the character char c = 'A'; // coverting it explicitly int i = (int) c; System.out.println("The ASCII value of " + c + " is " + i); } }
Output: The ASCII value of A is 65
Explanation:
This program declares a char variable c and assigns it the value of ‘A’. Then it declares an int variable i and assigns it the value of c with an explicit cast.
The (int) before c is called a cast operator, it tells the compiler to treat the value of c as an int rather than a char. Since a char is smaller than an int, the cast is allowed.
When you run this program, it will print out “The ASCII value of A is 65”, showing that the char variable c has been explicitly converted to an int and that its value is 65 which is the ASCII value of ‘A’.
3. Using the Character.getNumericValue() method:
The Character.getNumericValue() method can be used to convert a char to an int
Example :
import java.util.*; public class Main{ public static void main(String[] args) { // Initializing the character char c = 'A'; // coverting it using getNumericValue int i = Character.getNumericValue(c); System.out.println("The ASCII value of " + c + " is " + i); } }
Output: The ASCII value of A is 10
Explanation:
This program declares a char variable c and assigns it the value of ‘A’. Then it declares an int variable i and assigns it the value returned by Character.getNumericValue(c).
The Character.getNumericValue(c) method returns the int value that the specified character represents. A character’s int value, generally referred to as its “code point,” ranges from 0 to 65535.
When you run this program, it will print out “The Unicode value of A is 65”, showing that the char variable c has been converted to an int using the Character.getNumericValue() method and that its value is 65 which is the Unicode value of ‘A’.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment