Char to Int in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

There are different ways to convert a character to its corresponding integer representation.

  • Assigning a character to an integer-type variable gives the ASCII value of the character.
  • Use Character.getNumericValue() method
  • Convert the character to a String using String.valueOf() method and use Integer.parseInt() method.
  • Subtract '0' from the character.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

How to convert char to int in Java?

Sometimes we need to perform operations on integer values, here in this case it may be possible that the int value is stored in a char data type. In this situation, we first need to convert the char value to its corresponding integer value and then do the required operation.

The primitive data type can be converted from char to int data type in java using several ways:-

  1. Using ASCII values: We can automatically convert a character to its ASCII value by using implicit typecasting.
  2. Character.getNumericValue(): We can convert char to int using getNumericValue() method of the Character class.
  3. Using parseInt() method with String.valueOf(): We can convert char to int in java by converting char to string using String.valueOf() and then converting string to int using Integer.parseInt() method.
  4. Subtracting with '0': we can convert char to int by subtracting with 0, this works on the concept of ASCII values.

Method 1: Using ASCII values

American Standard Code for Information Interchange is an encoding format used to store characters on a computer, it is a 7- bit character set containing 128 characters.

In Java programming language we have implicit typecasting, i.e if we store the value of a smaller datatype to a variable of the larger datatype, then the value is automatically converted. This means that the variable is implicitly typecasted into a larger datatype variable.

Example:

Output:

Explanation:

  • So in this example variable first is implicitly typecasted to int datatype and then stored in second variable.
  • So now if we print the second variable it will print 53. This is because we have assigned the char variable first to the int variable second and we get the ASCII value of 5 i.e. 53.

Example: 53-48 = 5

Below is the java program to convert char to int in java using the ASCII value:-

Output:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Method 2: Using Character.getNumericValue()

In Java, we have a method getNumericValue() inside the Character class. This method returns an int value which is the encoding of a Unicode character.

Syntax:

This method takes an argument of char datatype and returns the corresponding int value.

Example:

Output:

Method 3: parseInt() method with String.valueOf()

In java we can also convert from char to int java using the parseInt() and String.valueOf() methods.

  • The String.valueOf(): method converts a char datatype variable into a string datatype variable and,
  • Integer.parseInt(): method is used to convert string into an int datatype.

The method valueOf() belongs to String class and parseInt() method belongs to Integer class.

Example:

Output:

Explanation:

  • Here in this example, ch1 is first converted to string using String.valueOf() and then it is converted to int using Integer.parseInt().
  • Similar operations are performed with ch2 character as well.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Method 4: Subtraction with '0':

In Java we can also convert the numeric value stored in a char to int datatype by subtracting it with the character 0(zero). This is based on the concept of ASCII values.

If we want to convert char to int, we will subtract '0' from the character.

Example:

Output:

Here,

  • In the code above, ch1 which is equal to '5' has an ASCII value of 53.
  • And ASCII value of 0 is 48 so if we subtract 48 from 53 we get 5 which is the int value of num1.
  • So in background implicit type casting is being done and the we subtract it from ASCII value of 0 and hence getting the conversion from char to int.
  • Similar operations are performed in case of ch2 as well.

Conclusion:

  1. In this article, we discussed different methods to convert char to int in java.
  2. These methods are:
    • Using ASCII values
    • Character.getNumericValue()
    • char to int using parseInt() method with String.valueOf()
    • char to int by subtracting with 0
  3. When a character is subtracted from another character, internal typecasting is performed to convert the characters to their corresponding ASCII numbers. The result is the subtraction between their ASCII values.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more