Write a Java Program to convert char type variables to int

Java Program to convert char type variables to int

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to convert char type variables to int
Java Program to convert char type variables to int

Write a Java Program to convert char type variables to  int

PROGRAM: Java Program to convert char type variables to int

/*Java Program to convert char type variables to int*/

1: Java Program to Convert char to int

class Main {
public static void main(String[] args) {

// create char variables
char a = ‘5’;
char b = ‘c’;

// convert char variables to int
// ASCII value of characters is assigned
int num1 = a;
int num2 = b;

// print the values
System.out.println(num1); // 53
System.out.println(num2); // 99
}
}

2: char to int using getNumericValue() method

class Main {
public static void main(String[] args) {

// create char variables
char a = ‘5’;
char b = ‘9’;

// convert char variables to int
// Use getNumericValue()
int num1 = Character.getNumericValue(a);
int num2 = Character.getNumericValue(b);

// print the numeric value of characters
System.out.println(num1); // 5
System.out.println(num2); // 9
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *