×

How to Convert int to char in Java

To convert a higher data type to lower data type, we need to do typecasting. Casting is also required when we want to convert ASCII value into character. It is called an explicit conversion. There are two methods to convert int to char:

  • Using typecast operator
  • Using forDigit() method

Using typecast operator

Casting is when we explicitly convert from one primitive data type, or a class, to another.

Example

In this example, we have initialized 65 to the variable a. 65 is the ASCII value of A. We cast the number from an integer to char, converting it to the letter A.

public class inTocharExample
{
public static void main(String args[])
{
int a=65;
char ch=(char)a;
System.out.println("The character value of  an integer is: "+ch);
}
}

Output

The character value of an integer is: A
 

Remember: If you are assigning 1 to a variable and want to convert it into character, then nothing will be print on the console. Because it will store ASCII value of 1 that is SOH (start of heading) which is a non-printable character.

Example

In the following example, we have done the same as above mentioned. We have initialized 1 to the variable i of type int. In the next statement, we performed casting. In the println statement, we are trying to print the converted value of 1, but it will not print the character ch.

public class inTocharExample1
{ 
public static void main(String args[])
{ 
int i=1;    
char ch=(char)i;    
System.out.println("Nothing will be print "+ch); 
}
}

Output

Nothing will be print

To remove the above problem, we will add '0' (as a character not as a digit) to the integer i during casting. It returns the actual value in the character form. The ASCII value of '0' is 48. When we add 48 to 1, it becomes 49, which is the ASCII value of 1.

Example

public class inTocharExample2
{  
public static void main(String args[])
{  
int i=1;    
char ch=(char)(i+'0');    
System.out.println("Character value is: " +ch); 
}
}

Output

Character value is: 1

When we initialize '1' as a character to the variable i of type int, it will store actual character in ch variable and print the same.

Example

public class inTocharExample3
{  
public static void main(String args[])
{  
int i='1';    
char ch=(char)i;    
System.out.println("Character value is: " +ch); 
}
}

Output

Character value is: 1

Using forDigit() method

forDigit() is a built-in static method of Character class which belongs to java.lang package. It parses two arguments digit and radix and returns the character representation of digit in specified radix format.  It returns null if the digit or radix are invalid numbers.  The signature of the method is given below.

public static char forDigit(int digit, int radix)

Invalid conversions

The following conversions returns null.

char ch2=Character.forDigit(3,2);                    //Binary radix

char ch2=Character.forDigit(9,8);                  //Octal radix

char ch2=Character.forDigit(10,10);      //decimal radix    

char ch2=Character.forDigit(16,16);  //Hexa radix

Valid Conversions

char ch2=Character.forDigit(0,10);    

char ch2=Character.forDigit(15,16);    //returns f because decimal value of f is 15

We can conclude from the above conversions that digit must be less than the radix.

Example

public class inTocharExample4
{ 
public static void main(String args[])
{ 
char ch1=Character.forDigit(9,10);    //Octal radix format
char ch2=Character.forDigit(11,16);                 //Hexa radix format
System.out.println("9 representations in radix 10 format: "+ch1);   
System.out.println("11 representations in radix 16 format: "+ch2);
}
}

Output

9 representations in radix 10 format: 9
11 representations in radix 16 format: b

Related Topics

Java Math rint() Method

The rint() method of Java Math class returns the double value which is close to the specified argument and is equal to mathematical integer. Syntax: public static double rint(double a) Parameters: The parameter ‘a’...

1 minute read.

How to Convert int to char in Java

To convert a higher data type to lower data type, we need to do typecasting. Casting is also required when we want to convert ASCII value into character. It is...

3 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java Command Line Arguments

Java command line arguments Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and...

2 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Java Math.multiplyExact() method

In java, we are provided with multiplyExact method in the Math module. This Math module belongs to the java.lang package. In general, this method returns the product of the provided...

2 minutes read.

Program to find and replace characters on string in java

In JAVA, Strings are immutable. To overcome this Java String class contains a lot of methods to do operations on Strings. One such method is replace method. String Replace It returns a...

3 minutes read.

Java program to find frequency of characters in a string

In this article, you will understand the how to find the frequency of characters in strings by using Java programming language. Along with this, you will understand the hashing concept...

3 minutes read.

Java Architecture

Java architecture is a combination of three parts they are JVM, JRE and JDK. These components will help in the functioning of the java programs. The process of code interpretation...

6 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

Java Garbage Collection Interview Questions

One of the key areas of Java is garbage collection. Garbage collection enables apps to manage memory automatically. Interviewers frequently ask inquiries about garbage collection. Q1: What is the purpose of...

6 minutes read.

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.