char Datatype in Java

Java Programming Language / DataTypes in java

2611

  • 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:

 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

   }
}

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.


This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.