Java Program for Int to Char Conversion


Java has eight primitive datatypes byte, short, int, long, char, float, double, and boolean. The int is 32-bit signed datatype used to store whole numbers. Its range is from –2,147,483,648 to 2,147,483,647. The char holds 16-bit unsigned Unicode characters.

In this article, we will discuss few approaches for int to char conversion −

  • By adding ‘0’

  • By using forDigit() method

  • By using typecasting

Example 1

The following example illustrates how to declare and initialize int variable.

int num1 = 24;

int is the keyword used to declare int variable, num1 is the name of variable that is holding value 24.

Example 2

The following example illustrates how to declare and initialize character variable. We store character variable in single quotation (‘ ’).

char ch1 = ‘S’;

char is the keyword used to declare character variable, ch1 is the name of variable that is holding ‘S’ character.

Approach 1: Using forDigit() method

The forDigit() method of class ‘Character’ returns a character for specified digit according to the given RADIX value. The RADIX value for decimal is 10, for hexadecimal it is 16 and for binary it is 2.

The class ‘Character’ is available in ‘java.lang’ package.

We will also use getClass().getSimpleName() method in our program to check the datatype of given int variables after converting to character.

Syntax of forDigit()

char char_variable = Character.forDigit(var_to_convert, radix);  

char_variable − name of character variable that will store the value after conversion.

Character is the class name and forDigit() is its method used along with two parameters.

var_to_convert is the variable that will get converted and radix acts as base for conversion

Example

import java.lang.*;
public class Conversion {
   public static void main(String[] args) {
      int rad = 16;
      int n1 = 8;
      int n2 = 11;
      // Conversion of int to char
      char ch1 = Character.forDigit(n1, rad );
      char ch2 = Character.forDigit(n2, rad );
      System.out.println("ch1: " + ch1);
      System.out.println("ch2: " + ch2);
      // to check the datatype
      System.out.println("type of ch1: " + ((Object)ch1).getClass().getSimpleName());
      System.out.println("type of ch2: " + ((Object)ch2).getClass().getSimpleName());
   }
}

Output

ch1: 8
ch2: b
type of ch1: Character
type of ch2: Character

In the above code, we have created three integer variables. The ‘ch1’ and ‘ch2’ will store the converted variables. We have used radix value as 16 therefore the specified variable has changed to hexadecimal numeric system (0 to 9 and after 9 A to F). The value at 11th position of hexadecimal system is ‘b’. Hence, we are getting 8 and ‘b’ as output. In the end we have checked the type of data.

Approach 2: Using Typecasting

When we convert a data type to another data type, we call it as type casting. There are two types of type casting in java explicit and implicit. In implicit typecasting a lower datatype is converted to any other higher datatype automatically by compiler and also during the process of conversion there is no risk of losing data.

When we typecast a higher datatype to a lower data type then, it is called explicit type casting and it is done manually. In it, there is a risk of losing data because lower datatype has range smaller than a higher datatype, this is the main reason for doing it manually.

In our program we will use explicit typecasting because integer range is higher than character.

Syntax of forDigit()

char_variable = (char) var_to_convert;   

Example

public class Conversion {
   public static void main(String[] args) {
      int n1 = 97;
      int n2 = 65;
      // Typecasting of int to char variable
      char ch1 = (char)n1;
      char ch2 = (char)n2;
      System.out.println("ch1: " + ch1);
      System.out.println("ch2: " + ch2);
   }
}

Output

ch1: a
ch2: A

In the above code, we have declared and initialized two integer variables ‘n1’ and ‘n2’. The ‘ch1’ and ‘ch2’ will store the converted variables. We are getting ‘a’ and ‘A’ in our output because they are the ASCII values of 65 and 97.

Approach 3: By adding ‘0’

When we add 0 in integer variable and typecast it then, it will actually return the ASCII value of that digit. 0 has the ASCII value of 48 so if we add 65 in 0 it will become 113 and ASCII value of 113 is q.

Example

public class Conversion {
   public static void main(String[] args) {
      int n1 = 65;
      int n2 = 66;
      // Conversion of int to char
      char ch1 = (char)(n1 + '0');
      char ch2 = (char)(n2 + '0');
      System.out.println("ch1: " + ch1);
      System.out.println("ch2: " + ch2);
      System.out.println("type of ch1: " + ((Object)ch1).getClass().getSimpleName());
      System.out.println("type of ch2: " + ((Object)ch2).getClass().getSimpleName());
   }
}

Output

ch1: q
ch2: r
type of ch1: Character
type of ch2: Character

Conclusion

In this article, we have discussed three approaches for int to char conversion. Most of the time we do this type of conversion by using explicit typecasting because it is easy to use and understand.

Updated on: 02-May-2023

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements