Java Programming/Keywords/char

char is a keyword. It defines a character primitive type. char can be created from character literals and numeric representation. Character literals consist of a single quote character (') (ASCII 39, hex 0x27), a single character, and a close quote ('), such as 'w'. Instead of a character, you can also use a unicode escape sequences, but there must be exactly one.

Syntax:

char variable name1 = 'character1';
Example Code section 1: Three examples.
  1. char oneChar1 = 'A';
    
  2. char oneChar2 = 65;
    
  3. char oneChar3 = '\u0041';
    
  4. System.out.println(oneChar1);
    
  5. System.out.println(oneChar2);
    
  6. System.out.println(oneChar3);
    
Computer code Output for Code section 1
A
A
A

65 is the numeric representation of character 'A' , or its ASCII code.

The nominal wrapper class is the java.lang.Character class when you need to store a char value but an object reference is required.

Example Code section 2: char wrapping.
  1. char aCharPrimitiveType = 'A';
    
  2. Character aCharacterObject = aCharPrimitiveType;
    

See also:

Last modified on 16 January 2013, at 19:02