Home  >  Article  >  Java  >  Java program implements the function of converting integers to characters

Java program implements the function of converting integers to characters

WBOY
WBOYforward
2023-08-20 11:29:331426browse

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 Chinese translation of

Example 1

is:

Example 1

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.

Example 2

is translated as:

Example 2

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'.

Method 1: Use forDigit() method

The

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() syntax

char 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

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 created three integer variables. 'ch1' and 'ch2' will store the converted variables. We use the base value 16, so the specified variable has been converted to the hexadecimal number system (0 to 9, then A to F). The value of bit 11 in the hexadecimal system is 'b'. Hence, we got 8 and 'b' as output. Finally, we checked the type of data.

Method 2: Use type conversion

When we convert one data type to another data type, we call it type conversion. There are two types of type conversions in Java: explicit and implicit. In implicit type conversion, the lower data type is automatically converted to any other higher data type by the compiler without risk of data loss during 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;   

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 declare and initialize two integer variables 'n1' and 'n2'. 'ch1' and 'ch2' will store the converted variables. We get 'a' and 'A' in the output because they are the ASCII values ​​of 65 and 97.

Method 3: By adding ‘0’

When we add 0 to the integer variable and do the type conversion, the ASCII value of the number is actually returned. The ASCII value of 0 is 48, so if we add 0 to 65, it becomes 113, and the 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

in conclusion

In this article, we discussed three methods to convert int to char. Most of the time, we use explicit type conversions for this type of conversion because it's easy to use and understand.

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete