English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java list (List)

Java Queue (queue)

Java Map collection

Java Set collection

Java input output (I/O)

Java Reader/Writer

Java other topics

Java program converts int type variable to char

    Comprehensive Java Examples

In this program, we will learn how to convert an integer (int) variable to a character (char) in Java.

To understand this example, you should understand the followingJava programmingTopic:

Example1: Java program to convert int to char

class Main {
  public static void main(String[] args) {
    //Create an int type variable
    int num1 = 80;
    int num2 = 81;
    //Convert int to char
    //Explicit type conversion
    char a = (char)num1;
    char b = (char)num2;
    //Print the value
    System.out.println(a);    // P
    System.out.println(b);    // Q
  }
}

In the above example, we have an int type variable num1and num2. Note this line,

char a = (char)num1;

Here, we use type conversion to convert an int type variable to a char type variable. For more information, please visitJava Type Conversion.

Please note that these int values are considered as ASCII values. Therefore, we getPhas an int value80and Q has an int value of 81. This is because P and Q The ASCII values are 80 and 81.

Example2: Convert int to char using forDigit() method

We can also use the forDigit() method of the Character class to convert an int type variable to a char type.

class Main {
  public static void main(String[] args) {
    //Create an int type variable
    int num1 = 1;
    int num2 = 13;
    //Convert int to char
    //For 0-9between the values
    char a = Character.forDigit(num1, 10;
    //For 0-9between the values
    char b = Character.forDigit(num2, 16;
    //Print the value
    System.out.println(a);    // 1
    System.out.println(b);    // d
  }
}

Note the expression

char a = Character.forDigit(num1, 10;

We have already used the forDigit() method, which converts a specified int value to a char value.

here,10and16are the base values for decimal numbers and hexadecimal numbers. That is, if the int value is between 0 and9we will10as the base value; if the int value is between 0 and15between, we will use16and so on.

Example3: By adding a char character with the character "0", convert char to int

In Java, we can also convert an integer" 0"Add to an integer to convert it to a character. For example,

class Main {
  public static void main(String[] args) {
    //Create an int type variable
    int num1 = 1;
    int num2 = 9;
    //Convert int to char
    char a = (char)(num1 + '0');
    char b = (char)(num2 + '0');
    //Print the value
    System.out.println(a);    // 1
    System.out.println(b);    // 9
  }
}

In the above example, please note the following lines:

char a = (char)(num1 + '0');

Here, the character" 0"is converted to ASCII value 48. The value 48 with num1That is1). The result is 49 Its ASCII value is1. Therefore, we will add the character '1' as output.

Note: This applies only to int valuesFrom 0 to9.

Comprehensive Java Examples