Using Characters

char type variables store characters. Java char is a 16-bit type.

The range of a char is 0 to 65,536.

There are no negative chars.

Characters in Java can be converted into integers and manipulated with the integer operators.

Java uses Unicode to represent characters.

More information about Unicode can be found at http://www.unicode.org.

Here is a program that demonstrates char variables:


public class Main {
  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);
  }
}

This program displays the following output:


ch1 and ch2: X Y

ch1 is assigned the int value 88, which is the ASCII and Unicode value that represents the letter X.

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.