Java Convert int to char7 Dec 2024 | 6 min read We can convert int to char in Java using typecasting. In order to convert higher data type into lower data type, we need to perform typecasting. Here, the ASCII character of integer value will be stored in the char variable. ![]() To get the actual value in char variable, we can add '0' with int variable. Alternatively, we can use Character.forDigit() method. Java int to char Example: Using Type castingLet's see the simple code to convert long to int in Java. File Name: IntToCharExample1.java Output: A But if we store 1, it will store ASCII character of given number that is start of heading which is not printable. So it will not print anything on the console. File Name: IntToCharExample2.java Output: 1 If we add '0' with int variable, it will return actual value in the char variable. The ASCII value of '0' is 48. So, if we add 1 with 48, it becomes 49 that is equal to 1. The ASCII character of 49 is 1. File Name: IntToCharExample3.java Output: 1 If we store integer value in a single quote, it will store actual character in char variable. File Name: IntToCharExample4.java Output: 1 To get the actual value, we can also use Character.forDigit() method. File Name: IntToCharExample5.java Output: 1 To get the hexa value, use redix 16 in Character.forDigit() method. File Name: IntToCharExample6.java Output: a Let's see some other popular ways to convert int to char Method 1: Using Switch CaseFile Name: IntToCharSwitchCase.java Output: The integer 1 is converted to character: a The integer 2 is converted to character: e The integer 3 is converted to character: i The integer 4 is converted to character: o The integer 5 is converted to character: u Method 2: Using Concept of Type-CastingFile Name: IntToCharTypeCasting.java Output: The ASCII value 65 is converted to character: A The ASCII value 66 is converted to character: B The ASCII value 67 is converted to character: C The ASCII value 97 is converted to character: a The ASCII value 98 is converted to character: b The ASCII value 99 is converted to character: c In this program, the for-each loop iterates through an array of integers. Each integer in the array represents an ASCII value. Inside the loop, the integer value is cast to a char using (char) value. This casting operation converts the integer, which is assumed to be a valid ASCII or Unicode code point, directly into the corresponding character. The converted character is then printed to the console. Method 3: Using Modulo OperatorFile Name: IntToCharModulo.java Output: The number 1 is converted to character: A The number 26 is converted to character: Z The number 27 is converted to character: A The number 52 is converted to character: Z The number 53 is converted to character: A Method 4: Using String Array and IndexingFilename: IntToCharStringArray.java Output: The number 1 is converted to character: A The number 3 is converted to character: M The number 5 is converted to character: P The number 2 is converted to character: X The number 4 is converted to character: K Java int to char MCQ1. What is the Unicode code point for the character 'A'?
Answer: A Explanation: The Unicode code point for the character 'A' is 65. 2. Which of the following expressions correctly converts an int variable num to a char in Java?
Answer: A Explanation: Casting an int to a char using (char) is the correct way to convert an int to a char in Java. 3. What will be the result of the following code?
Answer: A Explanation: The integer value 65 corresponds to the character 'A' in Unicode, so after casting, ch will be 'A'. 4. Which method can be used to convert an int to a char while preserving its Unicode value in Java?
Answer: A Explanation: Casting an int to a char using (char) preserves its Unicode value in Java. 5. What is the result of the following code?
Answer: B Explanation: Adding 1 to the integer value 97 results in 98, which corresponds to the character 'b' in Unicode, so after casting, ch will be 'b'. Next TopicJava String to boolean |
Binary to Decimal
Java Convert Java's binary to decimal conversion process entails changing a number's binary representation (base 2) into its decimal representation (base 10). Using the Integer.parseInt() function, which translates a binary text to its decimal equivalent, is one popular technique. Integer.parseInt("1010", 2), for instance, would provide 10. Using...
4 min read
Java String to float
We can convert String to float in Java by using the Float.parseFloat() method. Scenario It is generally used if we have to perform mathematical operations on the string that contains float number. Whenever we get data from textfield or textarea, entered data is received as a string....
4 min read
Java String to Date
In Java, a string variable can be represented in the format of date. The task is to translate or change the String variable into date format. Let's understand in detail the various methods and approaches used for the conversion of a String variable into a...
7 min read
Date to Timestamp
Java Convert Handling dates and times is an essential aspect of many applications. Java, being one of the most widely used programming languages, provides robust libraries and utilities for working with dates and times. One common requirement developers encounter is the conversion of a date object...
4 min read
Decimal to Binary
Java Convert Converting a decimal number to binary is a fundamental operation in computer science and programming. Binary is a base-2 numeral system, meaning it only uses two digits, 0 and 1. In contrast, the decimal system, which we use daily, is a base-10 numeral system...
17 min read
Java char to int
Java Convert char to int Converting a char to an int in Java is a fundamental operation when dealing with character data. In Java, a char is a 16-bit unsigned integer representing a single Unicode character, while an int is a 32-bit signed integer capable of...
5 min read
Java float to String
Java Convert float to String Converting a float to a String in Java is a fundamental operation often required in programming, especially when you need to display or manipulate numerical data as text. This conversion can be done in several ways, each with its own advantages and...
9 min read
Java String to Object
Java Convert String to Object We can convert a String to an Object in Java with assignment operator. Each class is internally a child class of the Object class. So, we can assign string to Object directly. We can also convert String to Class type object using the...
7 min read
Java int to double
Java Convert int to double Converting data types is a common operation in Java programming, especially when dealing with numerical data. One such Conversion is from int (integer) to double. Conversion is crucial as it enhances accuracy in calculations and processes involving fractional numbers. The int data...
3 min read
Java long to String
In Java programming, converting data types is a common task, especially when dealing with different types of variables. When it comes to converting a long data type to a String, there are several approaches available, each with its own advantages and use cases. In this...
2 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
