Home >Java >javaTutorial >Java program implements the function of converting integers to characters
Java has eight basic data types: byte, short, int, long, char, float, double and boolean. int is a 32-bit signed data type used to store integers. It ranges from -2,147,483,648 to 2,147,483,647. char is a 16-bit unsigned Unicode character.
In this article, we will discuss some methods to convert int to char−
By adding ‘0’
By using the forDigit() method
By using type conversion
The following example illustrates how to declare and initialize an int variable.
int num1 = 24;
int is the keyword used to declare an int variable, and num1 is the name of the variable that holds the value 24.
The following example illustrates how to declare and initialize character variables. We store character variables in single quotes (‘ ’).
char ch1 = ‘S’;
char is the keyword used to declare a character variable, ch1 is the name of the variable that holds the character 'S'.
forDigit() method of class ‘Character’ returns the character of the specified number based on the given RADIX value. The decimal RADIX value is 10, the hexadecimal RADIX value is 16, and the binary RADIX value is 2.
Class 'Character' is available in 'java.lang' package.We will also use the
getClass().getSimpleName() method in the program to check the data type after converting the integer variable to character.
ForDigit() syntaxchar char_variable = Character.forDigit(var_to_convert, radix);
char_variable − The name of the character variable that stores the converted value.
Character is the class name, forDigit() is its method, used with two parameters.
var_to_convert is the variable to be converted, radix is used as the base of conversion
Exampleimport 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()); } }
ch1: 8 ch2: b type of ch1: Character type of ch2: Character
Method 2: Use type conversion
When we cast a higher data type to a lower data type, this is called explicit type conversion and needs to be done manually. In this case, there is a risk of data loss since the lower data type has a smaller range than the higher data type, which is the main reason to do it manually.
In our program we will use explicit type conversion because integers have a higher range than characters.
ForDigit() syntax
char_variable = (char) var_to_convert;
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); } }
ch1: a ch2: A
Method 3: By adding ‘0’
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()); } }
ch1: q ch2: r type of ch1: Character type of ch2: Character
The above is the detailed content of Java program implements the function of converting integers to characters. For more information, please follow other related articles on the PHP Chinese website!