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'.
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.
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.
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.
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,
Keep in mind that a single program may store multiple values in different instances so you need to set the values accordingly.
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.
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.
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.
In the given example, 4 bytes are converted to 8 bytes and widening conversion holds true in the following conditions –
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. 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
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.
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 –
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.
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.
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.
Receive Latest Materials and Offers on Java Course