Learn Java by Example: Java Program to convert char type variables to int

Java Program to convert char type variables to int

In this program, we will learn to convert the character (char) type variable into an integer (int) in Java.

 


Example 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
  }
}

In the above example, we have char type variables a and b. Note that we have assigned the char variables to int variables.

Here, instead of the characters, the ASCII value of the characters are assigned to the int variables. Hence, we get the value 53 (ASCII value of ‘5’) and 99 (ASCII value of ‘c’) as output.


Example 2: char to int using getNumericValue() method

We can also use the getNumericValue() method of the Character class to convert the char type variable into int type.


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
  }
}

Here, as we can see the getNumericValue() method returns the numeric value of the character. The character ‘5’ is converted into an integer 5 and the character ‘9’ is converted into an integer 9.

To learn more about the getNumericValue() method, visit Java getNumericValue() (Official Oracle Documentation).


Example 3: char to int using parseInt() method

We can also use the parseInt() method of the Integer class to convert the char type variable to an int type.


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

    // create char variables
    char a = '5';
    char b = '9';

    // convert char variables to int
    // Use parseInt()
    int num1 = Integer.parseInt(String.valueOf(a));
    int num2 = Integer.parseInt(String.valueOf(b));

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

Notice the expression,

Integer.parseInt(String.valueOf(a))

Here,

  • String.valueOf(a) – converts the char type variable a into a String
  • Integer.parseInt() – converts the string into an int

Note: The Integer.parseInt() method only works with string type variables. Hence, the character ‘a’ is converted into a String.


Example 4: char to int by subtracting with ‘0’

In Java, we can also convert the character into an integer by subtracting it with character 0. For example,


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

    // create char variables
    char a = '9';
    char b = '3';

    // convert char variables to int
    // by subtracting with char 0
    int num1 = a - '0';
    int num2 = b - '0';

    // print numeric value
    System.out.println(num1);    // 9
    System.out.println(num2);    // 3
  }
}

In the above example, notice the line,

int num1 = a -'0';

Here, we have subtracted the character ‘a’ by the character ‘0’. In this case, the characters are converted into integers. And subtracting a value by zero gives the same value. That is, 9 - 0 = 9.

Hence, we get the integer values 9 and 3 of the character ‘9’ and ‘3’ respectively.

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.