Java Program to Display Character

In the previous article we have discussed about Java Program to Check Character is Alphabet or Digit or Character

In this article we are going to see how to print/display any character by using Java programming language.

Java Program to Display Characters

There are various methods available for print or display the character. Let’s check one by one.

Method-1: Java Program to Display Characters By Taking Static Input Character

Approach:

  • Create a variable with character datatype and name it as  ch
  • Assign any character value to it.
  • Finally just put that variable name in print statement.
  • Result will be printed.

program:

public class Main 
{  
    public static void main(String[] args) 
    {  
        //create the character variable and assigned character-a to it.
        char ch='a';  
        //print the result.
         System.out.print("Result: "+ ch);  
    }         
}  
Output:

Result: a

Method-2: Java Program to Display Characters By Using Implicitly Typecast

Approach:

  • Declare a variable with character datatype and name it as  ch
  • Assigned any integer value to it.
  • The compiler implicitly typecast integer to character and store the corresponding ASCII value.
  • Finally just put that variable name in print statement.
  • Result will be printed.

Program:

public class Main
{  
    public static void main(String[] args) 
    {  
        //create the character variable and assigned integer-65 to it.
        char ch=65;  
        //print the result.
        System.out.print("Result: "+ch);  
    }  
}  

Output:

Result: A

Method-3:  Java Program to Display Characters By Using Explicitly Typecast

Approach:

  • Declare an integer variable n and assign a integer value to it of which we will get the corresponding character.
  • Declare a variable with character datatype and name it as ch and assign the integer variable n followed by (char) to it.
  • So that the compiler explicitly typecast integer to character and store the corresponding ASCII value.
  • Finally just put that variable name in print statement.
  • Result will be printed.

program:

public class Main 
{  
      public static void main(String[] args) 
    {  
        //create the integer variable and assigned integer-65 to it.
        int n=97;  
        //create the character variable and assigned the integer variable n followed by (char) to it.
        char ch=(char)n;  
        //print the result.
        System.out.print("Result: "+ch);  
    }  
} 
Output:

Result: a

Method-4: Java Program to Display Characters By Using Unicode System

Approach:

  • Declare a variable with character datatype and name it as  ch
  • Assign a Unicode value to it.
  • Every character has different Unicode so when we assigned the Unicode to any character datatype variable it automatically converted it to its corresponding character.
  • Finally just put that variable name in print statement.
  • Result will be printed.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //create the character variable and assigned a Unicode to it.
        char ch='\u0061';  
        //print the result.
        System.out.print("Result: "+ch);  
    }  
}  
Output:

Result: a

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: