The primitive data type char

  • Review:

    • A computer can only store (binary) numbers

    • Non-numeric information (e.g.: letters) can be represented using a code

  • The char data type in Java:

    • char data type represents a single character

    • The Unicode is used as the standard representation method for characters

  • A Unicode character chart can be found here:

  • Some interesting character codes within the Unicode:

      • English alphabet:
      • Greek alphabet:
      • Hebrew alphabet:
      • Chinese characters:

Defining and using char typed variables

  • Syntax to define a char typed variable:

      char  variableName;  // The value (= number) in variable will be
                           // interpreted as an Unicode
    

    Example:

       public static void main(String[] args) 
       {
           char letter = 'A';              // Will store 65 in variable
           System.out.println( letter );
    
           letter = 65;                    // Will store 65 in variable
           System.out.println( letter );
    
           letter = 946;           // 946 = Unicode for Greek letter beta
           System.out.println( letter );
         
           letter = 24373;         // My Chinese name
           System.out.println( letter );
       } 

DEMO: demo/04-Math+String/02-char/Intro.java

Size of Java's primitive data types

  • The memory cells used to store variables of primitive types:

      Data type        # Bytes
     ------------------------------------------
      double           8 bytes
      float            4 bits
    
      long             8 bytes
      int              4 bytes
      short            2 bytes
      byte             1 byte
    
      char             2 bytes 
    
      boolean          1 byte

A char typed variable contains an integer of 2 bytes or 16 bits

How are char symbols stored inside the computer ?

  • Remember:

    • Everything stored inside the computer (memory) is a (binary) number

  • A character is:

    • A (binary) number where the computer is "told" to interprete the number using the Unicode table.

  • Example:

       public static void main(String[] args) 
       {
         char x = 65; // Interprete number with Unicode
         System.out.println( x );
    
         int  y = 65;  // Interprete number as binary number
         System.out.println( y );
       } 

DEMO: demo/04-Math+String/02-char/Typing.java

Computations with char data

  • You can perform arithmetic operations on char typed values

  • Example: get the next letter:

    public class NextChar 
    {
       public static void main(String[] args) 
       {
         char c = 65;     // 'A'
     
         System.out.println(c);         // Print 'A'
         System.out.println( (int) c ); // Tell Java to print c as an int
    
         c = (char) (c + 1);            // Add 1 to c
         System.out.println(c);         // Prints 'B'
         System.out.println( (int) c ); // Tell Java to print c as an int
       }
    }  

DEMO: demo/04-Math+String/02-char/NextChar.java

Bit of history:   char in older programming languages...

  • Java was invented in 1995 and it's a pretty recent programming language

      • That's why Java decided to use the newer Unicode to represent characters

  • Older programming languages (such as C, C++) were invented before the Unicode:

      • That's why older programming languages will use the older ASCII code to represent characters


  • The ASCII code:

    • The ASCII code is an 8 bits code

      (Unicode is a 16 bits code)

    • The ASCII code only represent the English alphbet

    • The ASCII code is a subset (= contained by) the Unicode

Using the Unicode numbers

  • Unicode for some commonly used characters:

      Characters        Code value in decimal    Unicode value
     --------------------------------------------------------------------
      '0' '1' .. '9'    48 49 .. 57              \u0030 \u0031 .. \u0039
      'A' 'B' .. 'Z'    65 66 .. 90              \u0041 \u0042 .. \u005A
      'a' 'b' .. 'z'    97 98 .. 122             \u0061 \u0062 .. \u007A 

  • You can use any one of these expressions to represent the character '0' in Java:

          '0'  (preferred)     48      '\u0030'        

    Example:

         char c;
     
         c = '0';               // '0'  preferred !
         c = 48;                // '0'
         c = '\u0030';          // '0'

DEMO: demo/04-Math+String/02-char/Unicode.java

<
Special chacters:    '\t' and '\n'

  • The backslash (\) character is used in Java to denote some characters with special effects

  • Some commonly used characters that have special effect on the terminal:

     Character symbol    Code in decimal     Name of character
     -------------------------------------------------------------
        '\t'	     9			 TAB
        '\n'	     10			 Newline   

  • Example:

           System.out.print('a');
           System.out.print('\t');                
           System.out.print('c'); 
      
       Output:
      
               a        c

DEMO: demo/04-Math+String/02-char/Tab.java