Looking for ways to change java char to int. A quick tutorial on How to convert char to int in Java Programming language.
Java char to int image

What is char in Java?

In java, char is a primitive data type. It is used for declaring character type variable and method. By default, char type variables are of two bytes. char literals are always stored in single quote’s (‘ ‘).

What is int in Java?

int is a primitive data type in java. It is used for declaring integer type variable and method. By default, int type variables are of four bytes.

Since now you know what is char and int type in Java, let’s explore the ways to convert it.

1. Changing char to an int variable directly


public class CharToInt1 {

	public static void main(String[] args) {
		
		char ch='a';
		char ch1='*';
		int i=ch; //assigning char type variable to int type variable
		int j=ch1; //assigning char type variable to int type variable
		System.out.println(i);
		System.out.println(j);
	}
}

Output

direct conversion of char to int

Output for the above code direct conversion of char to int

In the above code, we are directly assigning a character to an integer variable (line-8 and line-9).

int i and int j stores the ASCII(American Standard Code For Information Interchange) value for the respective variables. ASCII table is provided for your reference.

Note: ASCII value for ‘a’ is 97 and ‘*’ is 42.

2. Using Character.getNumericValue() to convert char to int in Java


public class CharToInt2 {

	public static void main(String[] args) {
		char c='7';
		int value=Character.getNumericValue(c);
		System.out.println(c);
	}
}

Output

Output 2

Output 2 example of char to int in Java program

The Character.getNumericValue() returns the int value that the specified Unicode character represents. This is another way to convert char to int in java.

3. Using String.valueOf() in Java


public class CharToInt3 {

	public static void main(String[] args) {
		char c='8';
		int value=Integer.parseInt(String.valueOf(c));
		System.out.println(value);
	}
}

Output

8

Using the String.valueOf() the method we can convert any type of data into a string. Further, we can convert int to string, float to string, char to string, double to string, Object to string. In the above code snippet, we have first converted char to string. Then we have used Integer.parseInt() the method to get the integer value from the string.

As a result, for your better understanding of char to int conversion, we provided a Java problem statement along with the code solution.

Using Java problem statement

Q)Use two methods toUpperCase() and toLowerCase() to show the use of char to int conversion in java.


import java.util.Scanner;
public class CharToInt {

	public static void main(String[] args) {
		
		Scanner sc=new Scanner(System.in);
		String s="";
		System.out.println("Enter a String");
		s=sc.nextLine();
		CharToInt obj=new CharToInt();
		System.out.println("Enter 1 to convert string to Upper case and 2 to convert string to lower case");
		int check=sc.nextInt();
		switch(check)
		{
		case 1:
			obj.toUpperCase(s);
			break;
		case 2:
			obj.toLowerCase(s);
			break;
		default:
			System.out.println("wrong choice");
		}
	}

	public void toUpperCase(String s)
	{
		char[] ch=s.toCharArray();
		int ctr=0;
		for(int k : ch)
		{
			if(k>=97 && k<=122)
			{
				k=k-32;
				char conv=(char)k;
				ch[ctr]=conv;
				ctr++;
			}
			else
			{
				char conv=(char)k;
				ch[ctr]=conv;
				ctr++;
			}
		}
		String str=String.valueOf(ch);
		System.out.println("After converting to upper case, string is-->"+" "+str);
	}

	public void toLowerCase(String s)
	{
		char[] ch=s.toCharArray();
		int ctr=0;
		for(int k : ch)
		{
			if(k>=65 && k<=90)
			{
				k=k+32;
				char conv=(char)k;
				ch[ctr]=conv;
				ctr++;
			}
			else
			{
				char conv=(char)k;
				ch[ctr]=conv;
				ctr++;
			}
		}
		String str=String.valueOf(ch);
		System.out.println("After converting to lower case, string is -->"+" "+str);
	}
}

Output

Enter a String
abGHY%$@w#hgjk
Enter 1 to convert string to Upper case and 2 to convert string to lower case
2
After converting to lower case, string is --> abghy%$@w#hgjk

Explanation

Creating a class and taking inputs [line-3 to line-12]
  • At line-3 creating class CharToInt.
  • After that, at line-6 Taking input using Scanner class.
.toUpperCase() [line-27 to line-44]
  • The method is used to convert all lower case letter for a given string to upper case. Therefore it takes a string as a parameter.
  • Firstly, toCharArray() is used to convert a string to an array of character(line-28).
  • Secondly, an enhanced for loop is used to fetch array values.
  • Moreover, char to int conversion can be seen in line-30. k accepts the ASCII value of the character. Later k is updated by subtracting 32 from it.
  • Finally, k is assigned to a char variable(line-33).
.toLowerCase()[line-46 to line-65]
  • So, the function toLowerCase() has almost the same method body as toUpperCase().
  • In toUpperCase() we subtracted 32 from k. On the other hand, we summed up 32 in toLowerCase() [line-59].
At last – Method call

Finally, we used a switch case to call the methods.

In conclusion, We hope this tutorial helped you understand Java programming in a simple way  Keep learning keep sharing. Follow us on Facebook and Instagram.