Grab Deal : Flat 30% off on live classes + 2 free self-paced courses! - SCHEDULE CALL

- Java Blogs -

How to Convert Char to Int in Java

In this tutorial, you will basically learn how to convert the char data type to int in Java. The value contained in one data type can be converted to other based on conversion requirements, the same holds true for char to int conversion as well. Here is a simple example where char is converted to int with cast keyword. Casting is done when you wanted to convert non-compatible primitive data types explicitly.

We will discuss the meaning of all these terms like data types, primitive data types, implicit or explicit conversion, compatible or non-compatible data types, casting etc. in the blog later. Here is a quick example for the code to convert char to int in Java with cast keyword.

This example, we have a character ('a'), and we cast it to an integer. Printing this integer out will give us the ASCII value of 'a'.

What are Conversions in Java?

Conversions happen in our daily life. For example, currencies conversion from one country to another, measurement conversions or more. These conversions will not alter the value but it will definitely change the way how are they represented. Java has the following primitive built-in data types that can be converted to another as needed. Convert Char to Int in Java

Each data type is designed efficiently to store one of the three categories of information – it could be either number of various sizes or range like (Byte -> short -> int -> long - > Float -> double) etc. It may be used to store the char values. And the lastly, it is used to store the Boolean values either True or False.

When a program is executed in Java, there is at least one data type associated with each program that remains the same once it is initialized or declared in the beginning. In most of the situations, variables for specific data types are declared as they are needed and information flows smoothly without any need of performing type conversions.

Read: Top Java Developer skills To Have For Better Employment Opportunities

However, there are certain situations when it is necessary to move information from one data type to another where the concept of conversions come into existence. This type of conversions can either be complete implicitly or explicitly. Before we finally discuss the implicit or explicit conversions, let know the data types in detail first.

What are the Data types? Know Data Types in Detail

Data types are the storage containers in Java or any other programming language that are used to store the value of a particular type like integers, chars, floating values or Boolean values etc. Based on the requirements, the data types need to define in the beginning when program coding starts. The choice of data types for a particular variable depends on multiple things. In most of the cases, the decision is intuitive. For example,

  • If you wanted to store a numeric value then declare the data type like int,
  • For floating storage declare the data type as Float,
  • To store the text messages, declare the data type as the string,
  • To store a single character, declare the data type like char,
  • To store the binary values, either True or False, declare the data type as Boolean.

Keep in mind that a single program may store multiple values in different instances so you need to set the values accordingly.

Rules of thumb for defining the Data Types

  1. To declare a number, use int most of the time if possible. You should use the Float keyword only if the value contains the fraction part. As fractions are not the complete number, they are just the approximation and needs more computational resources to operate.
  2. Further, integers are divided into four major categories. These are the bit, byte, short, long etc. Again, you are advised to use int most of the time, and add byte, short, log identifiers, only if you have the strong reason for the same.
  3. Further, floating points are also divided into two major categories – these are the float and double. We use double most of the times and float is used only when you wanted to conserve the storage within in the range defined for the particular data type.
  4. Moving ahead, Boolean, string, and char had their own significance and they can be converted to other data types too when required but make sure that the result of conversion comes within the range of destination data type.

But using data types is not always easy and they can be further categorized as the primitive or non-primitive data type. Primitive data types are already defined in the Java and they are eight in total as given on the top of this blog while non-primitive data types refer to an object, you don’t have to go into depth of because here we are focusing on primitive data types majorly.

Implicit Conversions / Widening Conversions

When the value of a lower sized data type is converted to wider size without losing any important data then it is named as the implicit conversion or we are widening any data type based on the storage requirements.

Convert Char to Int in Java

Read: Top 105 Angular Interview Questions and Answers In 2023

This conversion is performed by the Java Virtual Machine (JVM) explicitly so know as the implicit conversion. See the example below where integer data type is converted to double.

Convert Char to Int in Java

In the given example, 4 bytes are converted to 8 bytes and widening conversion holds true in the following conditions –

  • When byte needs to be converted in short, int, float, double, long etc.
  • The short data type can be converted to long, float, or double etc,
  • Char could be converted to int, long, float, or double etc,
  • Int can be converted to long, float, or double etc.
  • Long can be converted to float or double.
  • Finally, the float can be converted to double only.

For example, conversion of char to int comes to this category where we are trying to widen the storage space from char (16-bits) to int (32-bits) but they are not compatible to each other so we need to use the word cast here to perform the conversion successfully. Convert Char to Int in Java Keep in mind that when you are trying to char data type to Boolean values then char contains 16 bits while Boolean contains only one but an implicit conversion is about widening storage space not narrowing so it will return an error in that case. To perform this action, let us look at one more popular conversion category i.e. Narrowing Conversions

Explicit Conversions / Narrowing Conversions

If a data type with large size is converted to a value of data size with narrower size then it is named as the narrowing conversions or explicit conversions. It is done by JVM implicitly but needs explicit type casting. See the example below where the double data type is converted explicitly to the integer.

Read: Hibernate Interview Questions And Answers

Double data type contains 64-bits for storage while int data type is limited to 32-bits only. So, we have reduced the storage space requirements up to half here. The explicit conversions hold true in the following conditions –

  • The short data type can be converted to char or byte.
  • Char data type can be converted to short or byte.
  • Int data type can be converted to char, short, or byte.
  • The long data type can be converted to char, short, or byte.
  • Float data type can be converted to char, short, or byte, long and int.
  • The double data type can be converted to char, short, or byte, long, float and int.

Rules of thumb for the explicit conversion

  • The conversion should be held explicitly.
  • Target should be defined within parenthesis only.

Automatic Conversion

This type of conversion is very much similar to the widening conversions that occur implicitly between compatible data types. Here are the thumb rules to follow for the automatic conversions.

  • First, two data types should be compatible.
  • The destination data type should be larger than the source ones.

So, what are compatible data types? This a category where the value of one data type can appear as the value of other expected data type or vice versa. Java is safe, robust, and a strong typed programming language. Also, every defined variable should be given a type to store the needed value. All assignments either explicit or implicit are checked for the type compatibility before it is performed actually otherwise any mismatch may result in an error as well.

To convert two incompatible data types, you should use the word cast. A cast is simply an explicit type of conversion that holds the target value. Implicit or explicit conversions for primitive data-type are frequently misunderstood by programmers. With this blog, you have a clear idea now how to convert any data type either implicitly or explicitly even if they are not compatible with each other like char to int conversion in Java.

fbicons FaceBook twitterTwitter lingedinLinkedIn pinterest Pinterest emailEmail


    JanBask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.

  • fb-15
  • twitter-15
  • linkedin-15


Trending Courses

Cyber Security Course

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security Course

Upcoming Class

3 days 19 Jul 2024

QA Course


  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA Course

Upcoming Class

3 days 19 Jul 2024

Salesforce Course


  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL
Salesforce Course

Upcoming Class

-0 day 16 Jul 2024

Business Analyst Course

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum
Business Analyst Course

Upcoming Class

4 days 20 Jul 2024

MS SQL Server Course

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design
MS SQL Server Course

Upcoming Class

4 days 20 Jul 2024

Data Science Course

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science Course

Upcoming Class

3 days 19 Jul 2024

DevOps Course


  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing
DevOps Course

Upcoming Class

7 days 23 Jul 2024

Hadoop Course


  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation
Hadoop Course

Upcoming Class

3 days 19 Jul 2024

Python Course


  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python Course

Upcoming Class

11 days 27 Jul 2024

Artificial Intelligence Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence Course

Upcoming Class

3 days 19 Jul 2024

Machine Learning Course

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning Course

Upcoming Class

38 days 23 Aug 2024

 Tableau Course


  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
 Tableau Course

Upcoming Class

3 days 19 Jul 2024

Search Posts


Receive Latest Materials and Offers on Java Course