2.5 Arithmetic and Characters

We saw in the previous chapter that it was possible to convert values between numerical forms and characters by using an assignment statement,possibly with a cast. It is also possible (and often useful) to do arithmetic involving char values.

Before we start doing arithmetic with characters, let us explore some aspects of the relationship in Java between integers and characters. Recall that all Java characters are stored as 16 bits using the Unicode encoding system. As an example, the character 'A' is stored as the pattern 0000 0000 0100 0001. If we consider this as a base 2 numeral, it has a base 10 value of 1 x 26 + 1 x 20 = 64 + 1 = 65. If we were to write int n = 'A'; then Java would assign the value 65 to n. The assignment statement copies the bit pattern for 'A' into the location reserved for n. Although the pattern of the bits does not change, the way that Java interprets the bit pattern does change - from the char value 'A' to the int value 65. (See Appendix C for more information about representation of numbers using different bases and the relationships between characters and integers.)

In Unicode, the letters of the alphabet have been assigned codes for which the numeric values are sequential. Thus the numeric value corresponding to 'B' is 66, that for 'C' is 67, and so on. The lower case letter characters: 'a', 'b', 'c', ... also have sequential numeric values (from 97 to 122). Using these properties, we can alter characters using arithmetic operators.

Example 1

If we have created a char value c by the declaration
	char c = 'A';
then the statement
	c++
will change the value of c to 'B'.

We must be very careful to obey Java's type conversion rules when we are doing arithmetic.

Example 2

The statements
	char c = 'A';
      	c = c + 1;
will produce an error message becuase the expression c + 1 combines a char and an int result. The assignment to the char variable c is an attempt to convert an int to a char - a narrowing conversion. We can correct the problem by writing the assignment statement in the form
	c = (char)(c + 1);
The numeric values of the encodings for the characters '0', '1',..., '9' are not 0, 1, ..., 9 but they are sequential values. We can use thus fact to convert between the char form of a digit and the numerical value of the digit. The next example shows how this can be done.


Example 3

Given the statement
	char c = '7';
then the statement
	int i = c - '0';
will assign to i the value 7 because the difference between the encoding of '7' and the encoding of '0' is 7.


Exercise 2.5

  1. State the integer value of each expression.
    1. 'E' - 'A'
    2. '5' - '0'
    3. 'm' - 'p'
    4. '3' - '8'
  2. State the value of each expression.
    1. (char)(5 + 't' )
    2. (char)('2' + 3)
    3. (int)( 'z' - 'v' )
    4. (int)(' A' + 'f' - 'D' - 'b')
  3. For each valid fragment, state what it does. If a fragment contains an error, explain the nature of the error and rewrite the fragment with the error corrected.
    1. char a = 'e';
      a--;
    2. char b = '4'; 
      b = b - 2;
    3. int c = 'g'; 
      --c; 
      c = c -'k';
    4. int d = '7'; 
      char dd; 
      dd = d++;
    5. int e = 'q' + 2; 
      e++; 
      char ee = e';
    6. char f = 'M'; 
      --f; 
      int ff = f-- - 'J';
  4. Write a Java program that will first prompt the user for an upper case letter of the alphabet, read the letter supplied by the user, and then print the letter along with its position in the alphabet. For example, if the input is 'D', the program should print

    D is at position 4 in the alphabet.