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
}
}
A
5
Press any key to continue . . .
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.
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.
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.