Convert char to int in Java

In this tutorial, we will learn how can we change char to int in Java.

There are many ways by which we can convert or change char to int. Some of the approaches are as follows:

(1) Using Implicit type conversion or typecasting.

(2) Using getNumericValue Function

(3) Subtracting with ‘0’.

(4) Parsing the String along with String.valueOf() function

 

Method 1: Implicit type casting

In Java, typecasting means giving higher preference to the bigger data type. For eg, int is bigger than char so if a character is stored in an integer type variable then it will automatically promote itself to the int type and returns the ASCII value of that particular character.

Lets understand this with a program:

CODE:

import java.io.*;
class Code{
    public static void main(String[] args)throws IOException{
        char ch='B';//character in char datatype 
        System.out.println("Character type -> "+ch);
        int ck=ch;//type casting of character to int resulting in the return of its ascii values
        System.out.println("ASCII value of "+ch+" is -> "+ck);

        
    }
}

OUTPUT:

Character type -> B

ASCII value of B is -> 66

Hence it is proven that typecasting converts the character to its ASCII value.

 

Method 2: Using getNumericValue( ) function

With of help of getNumericValue ( ) function we can automatically convert a numeric character to the numeric value itself. Let discuss it with a program:

CODE:

import java.io.*;
class Code{
    public static void main(String[] args)throws IOException{
        char ch='9';//character in char datatype 
        System.out.println("Character type -> "+ch);
        int ck=Character.getNumericValue(ch);
        System.out.println("Numeric value -> "+ck);

        
    }
}

OUTPUT:

Character type -> 9

Numeric value -> 9

So with the help of the function, we can get the exact numeric value of the integer itself.

 

Method 03: Subtracting by ‘0’

So for eg, lets take a character ‘4’ ASCII value is 52 and ASCII value of ‘0’ is 48. If ‘4’ is subtracted by ‘0’ which is stored in an integer variable then it will result in the number itself 4. Let’s get it more clear with a program.

CODE:

import java.io.*;
class Code{
    public static void main(String[] args)throws IOException{
        char ch='4';//character in char datatype 
        System.out.println("Character type -> "+ch);
        int ck=ch - '0';
        System.out.println("Numeric value -> "+ck);

        
    }
}

OUTPUT:

Character type -> 4

Numeric value -> 4

 

So the output clears the fact that we can subtract a numeric character by ‘0’ to get the number itself

 

Method 04 : Parsing the String along with String.valueOf() function

In this last method, we will be using the parseInt technique along with String.valueOf function.

In the parseInt the parameter passed has to be a String so we use the String.valueOf() to convert the character to String. We can use Character.to String( ) to convert character to String value. Let’s learn it with the help of a program.

CODE:

import java.io.*;
class Code{
    public static void main(String[] args)throws IOException{
        char ch='4';//character in char datatype 
        System.out.println("Character type -> "+ch);
        int ck=Integer.parseInt(String.valueOf(ch));
        int ck2=Integer.parseInt(Character.toString(ch));
        System.out.println("\nString.valueOf()Function -> "+ck);
        System.out.println("Character.toString() Function -> "+ck);

        
    }
}

OUTPUT:

Character type -> 4

String.valueOf()Function -> 4

Character.toString() Function -> 4

 

So Character.toString () function and String.valueOf( ) function does the same work ie, converts the the given character Literal to String. Only one difference lies that String.valueOf() function can converts any datatype to String while Character.toString() converts only character literal to String.

Hope you like the content and was helpful for your time.

Leave a Reply

Your email address will not be published.