Java/J2EE and SOA (346 Blogs) Become a Certified Professional
AWS Global Infrastructure

How to convert Char to Int in Java?

Last updated on Jul 13,2020 3.8K Views

22 / 29 Blog from Java Programs

In Java, the value contained in one data type can be converted to another data type based on conversion requirements, the same holds true for char to int conversion. Converting a char to an int is equivalent to finding the ASCII value of the given character. In this article on “Convert Char to Int in Java”, we will learn how to convert a char to int with the help of examples. 

The topics discussed in this article are:

What are Data types?

Data types are the storage containers that store the value of a particular type like integers, chars, floating values, Boolean values, etc. A data type specifies the type of value a variable has and the type of relational, mathematical or logical operations that can be applied to it without causing an error. 

The choice of data types for a particular variable depends on multiple factors. In most cases, the decision is intuitive. For example,

  • If you want to store a numeric value then declare the data type like int
  • For floating storage declare data type as Float
  • For storing text messages, declare data type as the string
  • To store a single character, declare data type like char
  • For storing binary values, either True or False, declare the data type as Boolean

Java char to int: Getting ASCII value

Filename: CharToIntExample.java

Code:

public static void main(String args[]){  
char c='Z';  
char c1='9';  
int a=c;  
int b=c1;  
System.out.println(a);  
System.out.println(b);  
}}  

Compile by : javac CharToIntExample.java
Run by : java CharToIntExample

Output

Java char to int output | Edureka | Edureka Blogs       

Java char to int Example: Character.getNumericValue()

Filename: CharToIntExample1.java

Code:

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

Compile by: javac CharToIntExample1.java
Run by: java CharToIntExample1

Output:

Java char to int output2 | Edureka | Edureka Blogs

Java char to int Example: String.valueOf() :

Filename: CharToIntExample2.java

Code:

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

Compile by: javac CharToIntExample2.java

Run by: java CharToIntExample2

Output:

Java char to int output3 | Edureka | Edureka Blogs 

With this, we come to an end of this article. I hope it added value to your knowledge.

If you are interested to learn more about Java, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Upcoming Batches For Java Certification Training Course
Course NameDate
Java Certification Training Course

Class Starts on 28th January,2023

28th January

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th February,2023

25th February

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.