Skip to main content
Lesson 3 - Data Types in Java
Lesson MenuPreviousNext
  
ASCII Code Values and Character Data page 7 of 11

  1. As mentioned earlier, a character value can easily be converted to its corresponding ASCII integer value.

  2. A character value is stored using two byte of memory, which consists of 16 bits of binary (0 or 1) values.

  3. The letter 'A' has the ASCII value of 65, which is stored as the binary value 0000000001000001. This is illustrated in the following program fragment:

    char letter = 'A';
    System.out.println("letter = " + letter);
    
    System.out.print("its ASCII value = ");
    System.out.print((int)letter);
    
    Run output:
    
    letter = A
    its ASCII value = 65
    

    The statement (int)letter is called a type conversion. The data type of the variable is converted to the outer type inside of the parentheses, if possible.

  4. In Java, you can make a direct assignment of a character value to an integer variable, and vice versa. This is possible because both an integer and a character variable are ultimately stored in binary. However, it is better to be more explicit about such conversions by using type conversions. For example, the two lines of code below assign to position the ASCII value of letter.

    char letter = 'C';  // ASCII value = 67
    int position;
    
    position = letter;  // This is legal, position now equals 67
    

    vs.

    position = (int)letter;  // This is easier to understand.

    More detail about type conversions follows in the next section.


Lesson MenuPreviousNext
Contact
 ©ICT 2003, All Rights Reserved.