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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

How to convert Char to Int in Java?

Last updated on Sep 06,2024 4.5K 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.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to convert Char to Int in Java?

edureka.co