Java Program to Convert int into char

Java Program to Convert int into char

What is int in Java ?

“int” is a primitive data type in Java that is used to represent integers (whole numbers) within a certain range of values. The range of values that can be stored by an “int” is from -2^31 to 2^31-1. The “int” data type is commonly used in mathematical operations and to represent arrays of integers.

What is char in Java ?

“char” is a primitive data type in Java used to represent a single character. A character in Java is specified using single quotes, e.g. ‘a’, ‘b’, ‘c’, etc. The “char” data type is often used to store characters and strings of text. Each character is stored as a 16-bit Unicode character, which allows for representation of a wide range of characters and symbols.

Steps to Convert int into char  : 

Pseudo Code for the above algorithm.

int intValue = 65;
char charValue;
charValue = (char) intValue;

Example : 

Run

public class Main {
  public static void main(String[] args) {
    int[] intValues = { 65, 66, 67, 68, 69 };

    for (int intValue : intValues) {
      char charValue = (char) intValue;
      System.out.println("The char value of " + intValue + " is: " + charValue);
    }
  }
}

Output :

The char value of 65 is: A
The char value of 66 is: B
The char value of 67 is: C
The char value of 68 is: D
The char value of 69 is: E

Example : 

Run
class Main {
  public static void main(String[] args) {

    // create int variable
    int num = 97;

    // convert int to char using typecasting
    char ch = (char) num;

    // print value
    System.out.println(ch);    // a
  }
}

Output :

a

Example : 

Run
class Main {
  public static void main(String[] args) {

    // create int variables
    int num1 = 33;
    int num2 = 64;
    int num3 = 35;
    int num4 = 97;

    // convert int to char using typecasting
    char symbol1 = (char) num1;
    char symbol2 = (char) num2;
    char symbol3 = (char) num3;
    char symbol4 = (char) num4;

    // print values
    System.out.println("The symbol for " + num1 + " is: " + symbol1);
    System.out.println("The symbol for " + num2 + " is: " + symbol2);
    System.out.println("The symbol for " + num3 + " is: " + symbol3);
    System.out.println("The symbol for " + num4 + " is: " + symbol4);
  }
}

Output :

The symbol for 33 is: !
The symbol for 64 is: @
The symbol for 35 is: #
The symbol for 97 is: a

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription