Home / Programs / char Datatype in Java
🚀 Programming Example

char Datatype in Java

👁 2,676 Views
💻 Practical Program
📘 Step Learning
char Datatype in Java

📌 Information & Algorithm

  • char data type is a single 16-bit Unicode character
  • Minimum value is '\u0000' (or 0)
  • Maximum value is '\uffff' (or 65,535 inclusive)
  • Char data type is used to store any character
  • Example: char letterA = 'A'

💻 Program Code

 public class CharDataType {

   public static void main(String []args) {
      // this is declaration and initialization of variable letterA
	  // datatype is char
	  char letterA = 'A';
	  // this is declaration and initialization of variable letterB
	  // datatype is char
      char letterB = '5';
      System.out.println(letterA); // it will print letterA variable
      System.out.println(letterB); // it will print letterB variable

   }
}

                        

🖥 Program Output

A
5
Press any key to continue . . .
                            

📘 Explanation

This Java program demonstrates the use of the char data type. It declares and initializes two char variables, letterA and letterB, with the values 'A' and '5', respectively. Then it prints the values of these variables to the console using the System.out.println() method. This program shows that a char variable can hold a single character, which can be a letter, number, or symbol, and can be printed to the console using the println() method.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.