Characters in Java

Notes:

Characters in Java Programming Language :

Character:
- A symbol enclosed in between pair of single quotations is considered as a character in Java

Java allows us to work with both ASCII character set as well as Unicode character set.

ASCII character set:
- ASCII stands for American Standards Code for Information Interchange
- ASCII is a 7 bits character set
- Using ASCII character set we can represent maximum 128 characters
- Using ASCII character set we can represent characters available only in the English language
- ASCII character set has a unique number associated to each character in it

Unicode character set:
- Unicode stands for Universal code
- Unicode is a variable length character set (i.e. 8bits, 16bits or 32bits)
- Using Unicode character set we can represent almost all characters from almost all written languages present in the world
- Unicode character set has a unique code associated to each character in it

- To store and process characters; we take help of char data type
- char data type in Java is of 2 bytes (i.e. 16 bits); which is sufficient to store and process more than 65K+ characters

Syntax for declaring and initializing a character type variable:
char nameOfVariable = ’value’;
where
- value can be any symbol from the ASCII character set
- value can be any Unicode value from the Unicode character set

Ex:
char ch = ’A’;
OR
char ch = ‘\u0041’;

Example Code:

package charactersdemo;

public class CharactersDemo
{
public static void main(String[] args)
{
// char ch = 'a';
// System.out.println("ch = " + ch); // ch = a

// char ch = (char) 97;
// System.out.println("ch = " + ch); // ch = a

// char ch = '\u0041';
// System.out.println("ch = " + ch); // ch = A

// char ch = '\u0042';
// System.out.println("ch = " + ch); // ch = B

// char ch = '\u0021';
// System.out.println("ch = " + ch); // ch = !

// char ch = '\u00A9';
// System.out.println("ch = " + ch); // ch = ©

// char ch = '\u263A';
// System.out.println("ch = " + ch); // ch = ☺

// char ch = '\u2665';
// System.out.println("ch = " + ch); // ch = heart symbol

// char ch = '♥';
// System.out.println("ch = " + ch); // ch = ♥

// char ch = '\u0905';
// System.out.println("ch = " + ch); // ch = hindi A character

// char ch = 'अ';
// System.out.println("ch = " + ch); // ch = अ

// char ch = '\u0909';
// System.out.println("ch = " + ch); // ch = hindi U character

char ch = 'उ';
System.out.println("ch = " + ch); // ch = उ
}
}