Java character data type

   

Character data Type : Java Primitive Data Types

  1. In Java, the data type used to store characters is char.
  2. Character is 16 bits wide in Java.
  3. Java uses Unicode to represent characters.
  4. Java support lot of Unicode symbols from many more human languages for this purpose, it requires 16 bits.
  5. The range of a char is 0 to 65,536.
  6. There are no negative chars.

What is Unicode ?

Unicode defines a fully international character set that can represent all of the characters found in all human languages.

Live Example 1 : Integer Value Assigned to Character Data Type

class CharDemo {
  public static void main(String args[]) {
    char ch;

    ch = 'M';
    System.out.println("Character is : " + ch);

  }
}

Output :

Character is : M

Live Example 2 : Integer Value Assigned to Character Data Type

// Demonstrate char data type.
class CharDemo {
  public static void main(String args[]) {
    char ch1, ch2;

    ch1 = 88; // code for X
    ch2 = 'Y';

    System.out.print("ch1 and ch2: ");
    System.out.println(ch1 + "" + ch2);
  }
}

Output :

ch1 and ch2: X Y

Live Example 3 : Incrementing Character Variable

class CharDemo2 {
  public static void main(String args[]) {
    char ch1;

    ch1 = 'P';
    System.out.println("ch1 contains " + ch1);

    ch1++; // increment ch1
    System.out.println("ch1 is now " + ch1);
  }
}

Output :

ch1 contains P
ch1 is now Q

Other Data Types in Java : Integer | Float | Character | Boolean