5 approaches to print the ASCII value of character in java - CY

In this article I am going to discuss 5 different approaches to print the ASCII value of characters in java. codeYourslf - a programming blog
12 min read

print the ASCII value of character in java [5 different ways]

5 approaches to print the ASCII value of character in java - CY

In this article I am going to discuss 5 different approaches to print the ASCII value of characters in java.

Here I have used my android phone to do coding. Yes you hear right, I use mobile java ide i.e java N-IDE for coding. Please support.

Approach:-1 "print ASCII value of a character, just by changing the datatype"

 //By just changing the datatype

public class example1 {

    public static void main(String[] args){

      char alphabet1 = 'a';

      char alphabet2 = 'A';

      

      int alphabet3 = alphabet1;

      int alphabet4 = alphabet2;

      System.out.println("The ascii value of '"+ alphabet1+"' is "+ alphabet3);

      System.out.println("The ascii value of '"+ alphabet2+"' is "+ alphabet4);

    }

}

 
+
Explanation

ASCII(American Standard Code for Information Interchange) is the unique code for a character. 

It shows the numerical value of a character. 

Here alphabet1 and alphabet2 are in char datatype. 

I just created two int datatype alphabet3 and alphabet4, and I have assigned the value of alphabet1 and alphabet2 respectively. 

So it will print the ASCII value of 'a' and 'A'.

Output

print ASCII value of a character, just by changing the datatype

Approach:-2 "print ASCII value of a character, by typecasting concept"

//By typecasting

public class example2 {

    public static void main(String[] args){

      char a1 = 'z';

      char a2 = 'Z';

      

      int a3 = (int)a1;

      int a4 = (int)a2;

      System.out.println("The ascii value of '"+ a1+"' is "+ a3);

      System.out.println("The ascii value of '"+ a2+"' is "+ a4);

       

    }

}

 
+
Explanation

We know by typecasting we can change a variable from one datatype to another. 

Here I have changed the char datatype to int, as a result the ASCII value will be printed.

Output

print ASCII value of a character, by typecasting concept

Approach:-3 "Print the character by entering ASCII value in java"

//By direct receiving char value in int variable

import java.util.Scanner;

public class example3 {

    public static void main(String[] args){

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter the ascii value: ");

      int c = sc.nextInt();

      

      System.out.println(c+" is '"+(char)c+"'");

    }

}

  
+
Explanation

Here I will print the character, is you enter any correct ASCII value

Here also I have used the typecast concept.

Output

Print the character by entering ASCII value in java

Approach:-4 "Print all ASCII value of lower case alphabets in java"

//print all ascii value of lower case characters

public class example4 {

    public static void main(String[] args){

      for(int i = 97; i <= 122; i++){

        System.out.println("The ascii value of '"+ i+"' is "+ (char)i);

      }

    }

}

   
+
Explanation

Here I will print all the ASCII value of lower case character using for loop. 

The ASCII value of 'a' is 97 and that of 'z' is 122, so I have started the loop from 97 to 122 to print all the ASCII value.

Output

The ascii value of '97' is a

The ascii value of '98' is b

The ascii value of '99' is c

The ascii value of '100' is d

The ascii value of '101' is e

The ascii value of '102' is f

The ascii value of '103' is g

The ascii value of '104' is h

The ascii value of '105' is i

The ascii value of '106' is j

The ascii value of '107' is k

The ascii value of '108' is l

The ascii value of '109' is m

The ascii value of '110' is n

The ascii value of '111' is o

The ascii value of '112' is p

The ascii value of '113' is q

The ascii value of '114' is r

The ascii value of '115' is s

The ascii value of '116' is t

The ascii value of '117' is u

The ascii value of '118' is v

The ascii value of '119' is w

The ascii value of '120' is x

The ascii value of '121' is y

The ascii value of '122' is z

    

Approach:-5 "Print all ASCII value of upper case alphabets in java"

//print all ascii value of upper case character

public class example5 {

    public static void main(String[] args){

      for(int i = 65; i <= 90; i++){

        System.out.println("The ascii value of '"+ i+"' is "+ (char)i);

      }

    }

}   
+
Explanation

In this program, I have printed all the ASCII value of all upper case character. 

Do you know the ASCII value of 'A' is 65 and 'Z' is 90, so by taking the loop from 65 to 90 we can able to print all ASCII values.

Output

The ascii value of '65' is A

The ascii value of '66' is B

The ascii value of '67' is C

The ascii value of '68' is D

The ascii value of '69' is E

The ascii value of '70' is F

The ascii value of '71' is G

The ascii value of '72' is H

The ascii value of '73' is I

The ascii value of '74' is J

The ascii value of '75' is K

The ascii value of '76' is L

The ascii value of '77' is M

The ascii value of '78' is N

The ascii value of '79' is O

The ascii value of '80' is P

The ascii value of '81' is Q

The ascii value of '82' is R

The ascii value of '83' is S

The ascii value of '84' is T

The ascii value of '85' is U

The ascii value of '86' is V

The ascii value of '87' is W

The ascii value of '88' is X

The ascii value of '89' is Y

The ascii value of '90' is Z

    

Hey if you learn something new from this article please share with your friends.

And if you have any questions or any query, please feel free to write it on the comment below, I will definitely answer to your questions.

`

You may like these posts

Post a Comment

Hey Please 👏, feel free to share your opinion 🌍