Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Language Fundamentals - Character Literals

  • the char type represents 16-bit Unicode characters
  • Unicode is a superset of the ASCII character set which allows non-English language characters
  • any Unicode character can be written as a literal using the Escape character (backslash \) and it's hexadecimal representation
            '\udddd'    // where 'dddd' = hex digit (0 - F)
    
  • single characters are represented within single quotes
        'a'         // char literal
        '9'         // char literal
    
  • there are three exceptions that require the use of the Escape character
        single quote    ' \' '      displays as '
        double quote    ' \" '      displays as "
        backslash       ' \\ '      displays as \
    
  • there are certain special characters which can be represented by escape sequences
Esc Char Unicode Char Definition
\n \u000A newline
\t \u0009 tab
\b \u0008 backspace
\r \u000D return
\f \u000C form feed
\ddd   octal value
  • Octal character constants can have three digits or less (\000 through \377)

!!! Warning !!!
The compiler translates Unicode characters at the beginning of the compile cycle.

Using the Unicode escape characters \u000A for newline and \u000D for return in a String or comment produces a compile-error as they are interpreted, literally, as 'end-of-line'.

Always use the special characters '\n' or '\r'


Traps

  • String literal "c" assigned to char type
Source Package Import Class Interface Constructors
  Methods main() Identifiers Keywords Defaults Arrays
  Primitives # Literals char Literal String Literals Class Literals