Table of content

What is a char in Java?

The char keyword is a primitive data type that is used to store a single character value. It is used to declare the character type variables and methods.

It can hold the unsigned 16-bit Unicode characters. The range of char lies between 0 to 65,535. The minimum value is '\u0000'(0) and the maximum value is '\uffff'(65,535).

The size of the char is 2 bytes. The default value is '\u0000'. If we provide an integer value to a char variable, the compiler implicitly typecast integer to the corresponding ASCII value of the character.

To declare a character we use a char keyword. For example char c = 'A';

public class Example {
  public static void main(String[] args) {
    char char1 = 65;
    char c = 'U';
    System.out.println("Character 1: " + char1);
    System.out.println("Character 2: " + c);

The output is

Character 1: A
Character 2: U

String vs char

  • A string consists of a sequence of characters.
  • A string is a class.
  • A string literal needs to be enclosed in double quotes.
  • A string is declared as String str = "Java Program";
  • A char is a single character.
  • A char is a primitive data type.
  • A char literal needs to be enclosed in single quotes.
  • A char is declared as char x = "M";
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions