Using char
Data Type
class CharDemo { public static void main (String[] args) { //The char data type is represented internally as a number //It holds a single unicode character char correctAnserOption = 'A'; //The unicode value for the @ sign is 64. char atSign = 64; System.out.println(atSign); //You can also use it to display non-printable characters //You use a backslash to represent non-printable characters //Here is a tab key char tab = '\t'; System.out.println("Hello" + tab + "There"); //TODO //Try adding a newline instead of a tab between Hello and There //HINT - The newline character is represented by n } }
Character Literals
The char data type can contain a single unicode character enclosed in single quotes('').
char correctAnserOption = 'A';
Note that the 'A' character is enclosed in single quotes.
Non-printable characters
Char literals can also be created from the int literals. ```java char atSign = 64; char atSignInhexadecimal = 0x40; //Created using hexadecimal notation. Dont try at home. char tab = '\t'; //Defines a tab character